简体   繁体   English

SQL Server插入下一列

[英]SQL Server insert into next column

SQL Server 2012: is it possible to do a insert into select statement but insert data into the next column if the previous is already populated? SQL Server 2012:是否可以在select语句中执行插入操作,但如果前一列已被填充,则可以将数据插入下一列?

I have a table with a number of columns to store dates, I want to take data from another table and insert into the next available null date column. 我有一个带有许多列的表来存储日期,我想从另一个表中获取数据并插入到下一个可用的空日期列中。

If you are saying that the previous column is already populated, then you're really looking for some sort of update. 如果您说上一列已经填充,那么您实际上是在寻找某种更新。 Try this: 尝试这个:

UPDATE yourTable
SET
    col1 = CASE WHEN col1 IS NULL THEN 'value' ELSE col1 END,
    col2 = CASE WHEN col1 IS NOT NULL THEN 'value' ELSE col2 END;

The logic here is what you described, namely that we attempt to update col1 with some value. 您所描述的逻辑就是您所描述的,即我们尝试用一些值更新col1 Should that column be empty, we make the update, otherwise we update col2 instead. 如果该列为空,则进行更新,否则更新col2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM