简体   繁体   English

MS SQL Server INSERT值如果为NULL

[英]MS SQL Server INSERT value If NULL

Good day 美好的一天

I have Table1: 我有表1:

COLUMN1    COLUMN2   Column3
----------------------------
1             A        K
2             NULL     K
3             NULL     D
...           ...      ...

I need 我需要

COLUMN1    COLUMN2   Column3
----------------------------
1             A        A
2             K        K
3             D        D
...           ...      ...

I am stuck with my code and I cannot understand why server tries to insert into COLUMN3 ? 我坚持执行自己的代码,无法理解为什么服务器尝试将其插入COLUMN3?

Thanks for any opinions 感谢您的任何意见

INSERT INTO Table1 (COLUMN2)
    SELECT CLoumn3
    FROM Table1
    WHERE Column2 IS NULL; 

Resulting error: 产生的错误:

Msg 515, Level 16, State 2, Line 1 信息515,第16级,州2,第1行
Cannot insert the value NULL into column 'Column3', table 'TABLE1'; 无法将值NULL插入表“ TABLE1”的列“ Column3”中; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。

This is only example original table have more columns. 这只是原始表具有更多列的示例。

I think you want an update : 我认为您想要update

update table1
    set column2 = column3
    where column2 is null;

insert inserts new rows into a table. insert新行插入表中。 update changes the values in existing rows. update更改现有行中的值。

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

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