样本数据:
create table Table1 (KeyField int,FieldName varchar(20),Targetcolumntoupdate numeric(5,2));
create table Table2 (KeyField int,FieldName varchar(20),Sourcecolumntoupdate numeric(5,2));
insert into Table1 values (1,'F1',0.00);
insert into Table1 values (2,'F1',0.00);
insert into Table2 values (1,'F1',1.00);
insert into Table2 values (1,'F1',2.00);
您是否打算将 Table1 中的列更新为:
-- Wrong approach possible cause of error stated
Update Table1
Set Table1.Targetcolumntoupdate =
( SELECT (T2.Sourcecolumntoupdate)
FROM Table1 T1
INNER JOIN Table2 T2 ON
T1.KeyField = T2.KeyField AND T1.FieldName = T2.FieldName) ;
那么这是错误的,因为您可以看到为选择更新所需数据而创建的子查询 Targetcolumntoupdate 将返回多个可能的值。
正确查询如下:
Update T1
Set T1.Targetcolumntoupdate = (T2.Sourcecolumntoupdate)
FROM Table1 T1
INNER JOIN Table2 T2 ON
T1.KeyField = T2.KeyField AND T1.FieldName = T2.FieldName;
希望这可以帮助!!!