2016-04-28 22 views
-1

私は2列の行を更新する必要があり、正しい解決策を見つけることができない状況にあります。DBで行が正しく更新されないのはなぜですか?

私はこのようなテーブルがあります。

table1 : 

nid listName ltitle 
1  lsn1  lst1 
2  lsn2  lst2 

を、今このNIDがより良く理解するためにtable2の

table2 

nid listid listcol1 listcol2 
1 1  "lstxt1" "lscol1" //belongs to lsn1 
2 1  "lstxt2" "lscol2" //belongs to lsn1 
3 1  "lstxt3" "lscol3" //belongs to lsn1 
3 2  "lstxt4" "lscol4" //belongs to lsn2 

のために外部キー(listid)でlsn1とlsn2と最初の1という名前の2つのリストがありますlistcol1とlistcol2の2つの列があり、listcol1には というデータが含まれています。これは「###」で区切られた文字列で、同様に区切り文字として"lstxt1###lstxt2###lstxt3###"と同様にlistcol2になります。

01ユーザによって変更されたデータに保存されているprcodureに両方のテーブルを更新する

私の試みは、次のとおりです。ここ

update table1 set [email protected] ,[email protected] //table 1 update and it works well 
where [email protected] 

update table2 set [email protected], listcol1=a.part,listcol2= b.part from  //table2 update , problem is here 
dbo.splitstring(@listcol1,'###') a inner join  
dbo.splitstring(@listcol2,'###') b on a.id=b.id where [email protected] 

問題がある: (1)TABLE1が正しく更新されますが、出力が間違っている更新TABLE2(私は更新しようとしたと言うことができますNID = 1表1に)のために:

listcol1 listcol2 
"lstxt5" "lscol5" //belongs to lsn1 
"lstxt5" "lscol5" //belongs to lsn1 
"lstxt5" "lscol5" 

私はそれがすべての行に最初の行を更新する意味します。期待される出力がどこであったか:

listcol1 listcol2 
"lstxt5" "lscol5" //belongs to lsn1 
"lstxt6" "lscol6" //belongs to lsn1 
"lstxt7" "lscol7" 

(2)ユーザーが新しい行を追加すると、その行に更新が表示されません。それはまたtahtを示さなければならない。あなたにこのようなエラーを与える必要がありますlistcol1=a.part,listcol1= b.part

:(限り私はあなたのロジックを理解して)必ず何らかの意味がないため

答えて

0

コードのこの部分

メッセージ264、レベル16、状態1、行3 INSERTのSET句または列リストに列名 'listcol1'が複数指定されています。同じ節に複数の値を割り当てることはできません。句を変更して、列が1回だけ更新されるようにします。このステートメントによって列がビューに更新または挿入された場合、列のエイリアシングによってコード内の重複が隠されることがあります。 "

update table1 set [email protected] ,[email protected] //table 1 update and it works well 
where [email protected] 

update table2 set [email protected], **listcol1=a.part,listcol1= b.part** from  //table2 update , problem is here 
dbo.splitstring(@listcol1,'###') a inner join  
dbo.splitstring(@listcol2,'###') b on a.id=b.id where [email protected] 
関連する問題