表1
Column1 Column2
aa 12
bb 12
cc 12
aa 12
bb 12表2
Column1 Column2
aa 12
bb 13
cc 14
aa 15
bb 16现在,使用Sql,我需要使用基于table2的table2.Column 2将table1.Column 2更新为column1和table1.column1记录。
update Table1 a set a.Column2 = ( select b.Column2from Table2
b where A.column1= b.column1)上面的查询会引发错误:
列或变量中不允许的空值。
我试图在两个不同的系统之间复制数据。
发布于 2016-06-29 14:11:19
似乎有一些table1行没有任何匹配的table2行。在这种情况下,子查询返回NULL。我猜表1.列2不允许空值?
添加一个WHERE子句,只更新table2中匹配的行。
update Table1 a set a.Column2 = (select b.Column2 from Table2 b
where A.column1= b.column1)
where exists (select 1 from Table2 b2
where A.column1= b2.column1)发布于 2016-06-29 14:18:54
除了@jarih的答案。
如果您在当前支持的版本上,即。7.1或更高..。
你可以用MERGE
merge into table1 A
using (select column1, column2 from table2) as B
on a.column1 = b.column1
when matched then
update set a.column2 = b.column2https://stackoverflow.com/questions/38101883
复制相似问题