0

初めての投稿なのでお手柔らかに(;_;)

&Table1の 2 つの列の一致するデータに基づいて、列のフィールドを更新しようとしています。Table 1Table 2

列名は次のとおりです。

Table1.KeyField = Table2.KeyField
Table1.FieldName = Table2.FieldName

更新する列Table 1NumericValueゼロです。

私が試したすべての方法でエラーが発生しました:

サブクエリが複数の値を返しました。サブクエリが =、!=、<、<=、>、>= の後にある場合、またはサブクエリが式として使用されている場合、これは許可されません。

どんな助けでも感謝します。

ありがとう

4

2 に答える 2

1

サンプルデータ:

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;

お役に立てれば!!!

于 2013-10-11T10:26:55.237 に答える
1
update t
set    t.NumericValue = ???
from   Table1 t
join   Table2 t2
on     t.KeyField = t2.KeyField
and    t.FieldName = t2.FieldName
于 2013-10-11T09:59:12.540 に答える