SQL Server更新查询更新两个表

时间:2021-09-13 00:11:11

I am using SQL Server. I have written an update statement using a join. I try to update one column in the table, but my update statement removes the value from another table. I do not understand why it is happening even though I am updating value for one column.

我正在使用SQL Server。我使用连接编写了更新语句。我尝试更新表中的一列,但我的update语句删除了另一个表中的值。我不明白为什么它正在发生,即使我正在更新一列的值。

update rs
set rs.col1 = t.col2
from table1 r
join table2 rs on rs.Id = r.Id
join @temp t on t.Id = rs.Id

I am trying to update col1 value in table2 but it updates the value both in table2 for col1 and in table1

我正在尝试更新table2中的col1值,但它更新table2中的col1和table1中的值

2 个解决方案

#1


0  

I think you have a problem with your joins. I've also just renamed some of the Aliases of the tables to make the statement a bit more clearer.

我认为你的联接有问题。我也刚刚重命名了表中的一些别名,使声明更清晰一些。

update t2
set t2.col1 = t.col2
from table1 t1
    join table2 t2 on t2.id = t1.id
    join @temp t on t.Id= t2.Id

#2


0  

Cascade rules must available there on tables. On Update cascade delete cascade rule available on tables.

桌面上必须有级联规则。在表上可用的更新级联删除级联规则。

When you are updating column of one table by Cascade rule it is deleting value from another table. Check whether you have applied Cascade rule on table or not. If Cascade rule is applicable on both tables table1 and table2 then first remove that cascade rule and again try to update it.

当您通过Cascade规则更新一个表的列时,它将从另一个表中删除值。检查您是否在桌面上应用了Cascade规则。如果Cascade规则适用于表table1和table2,则首先删除该级联规则并再次尝试更新它。

#1


0  

I think you have a problem with your joins. I've also just renamed some of the Aliases of the tables to make the statement a bit more clearer.

我认为你的联接有问题。我也刚刚重命名了表中的一些别名,使声明更清晰一些。

update t2
set t2.col1 = t.col2
from table1 t1
    join table2 t2 on t2.id = t1.id
    join @temp t on t.Id= t2.Id

#2


0  

Cascade rules must available there on tables. On Update cascade delete cascade rule available on tables.

桌面上必须有级联规则。在表上可用的更新级联删除级联规则。

When you are updating column of one table by Cascade rule it is deleting value from another table. Check whether you have applied Cascade rule on table or not. If Cascade rule is applicable on both tables table1 and table2 then first remove that cascade rule and again try to update it.

当您通过Cascade规则更新一个表的列时,它将从另一个表中删除值。检查您是否在桌面上应用了Cascade规则。如果Cascade规则适用于表table1和table2,则首先删除该级联规则并再次尝试更新它。