如何将两个表和更新列连接在一起

时间:2016-03-22 14:54:44

标签: sql sql-server

表1:

idtb1 --pk
published
delete

表2:

idtb2 --pk
idtb1 --fk
published
delete

如何通过命令published一起更新2个表上的2列deleteUpdate

1 个答案:

答案 0 :(得分:4)

您无法在单个语句中更新多个表

       BEGIN TRANSACTION

       update A
       set A.published =  @published
       from table1 A 
       inner join table2 B on B.idtb1 = A.idtb1

      update B
      set B.published = @published 
      from table2 B 
      inner join table1 A on B.idtb1 = A.idtb1

      COMMIT

This question already has an answer here