Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 为什么数据库中的行更新错误?_Sql_Sql Server_Database_Sql Update - Fatal编程技术网

Sql 为什么数据库中的行更新错误?

Sql 为什么数据库中的行更新错误?,sql,sql-server,database,sql-update,Sql,Sql Server,Database,Sql Update,我的情况是,我必须更新两列中的行,但我无法找到正确的解决方案 我有这样的桌子: table1 : nid listName ltitle 1 lsn1 lst1 2 lsn2 lst2 现在这个nid是表2的外键(listid) table2 nid listid listcol1 listcol2 1 1 "lstxt1" "lscol1" //belongs to lsn1 2 1 "lstxt2" "lscol2" /

我的情况是,我必须更新两列中的行,但我无法找到正确的解决方案

我有这样的桌子:

table1 :

nid  listName ltitle
1     lsn1      lst1
2     lsn2      lst2
现在这个nid是表2的外键(listid)

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
为了更好地理解,有两个名为lsn1和lsn2的列表,第一个列表有两列listcol1和listcol2,listcol1包含来自 通过一个字符串,我将“#####”作为分隔符,类似于下面的
“lstxt1####lstxt2###lstxt3###”
类似于listcol2

我尝试在用户更改的数据上更新存储的prcodure中的两个表是:

update table1 set listName=@listName ,ltitle=@ltitle   //table 1 update and it works well
where nid=@nid  

update table2 set listId=@nid, 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 table2.listid=@nid  
这里的问题是: (1) 表1已正确更新,但表2更新的输出错误(假设我尝试更新表1中的nid=1):

我的意思是它会将第一行更新为所有行。其中,预期产出为:

listcol1 listcol2
"lstxt5" "lscol5" //belongs to lsn1
"lstxt6" "lscol6" //belongs to lsn1
"lstxt7" "lscol7"
(2) 当用户添加新行时,它不会在该行中显示更新。它也必须显示taht。

这部分代码(就我理解您的逻辑而言)肯定没有任何意义:
listcol1=a.part,listcol1=b.part

这会给您一个类似以下的错误:

table1 :

nid  listName ltitle
1     lsn1      lst1
2     lsn2      lst2
味精264,16级,状态1,第3行 在INSERT的SET子句或列列表中多次指定列名“listcol1”。一个列不能在同一子句中分配多个值。修改该子句以确保列只更新一次。如果此语句更新或向视图中插入列,则列别名可能会隐藏代码中的重复项。”

update table1 set listName=@listName ,ltitle=@ltitle   //table 1 update and it works well
where nid=@nid  

update table2 set listId=@nid, **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 table2.listid=@nid