How to update two tables in one statement in SQL Server 2005?

后端 未结 9 1397
轻奢々
轻奢々 2020-11-22 05:45

I want to update two tables in one go. How do I do that in SQL Server 2005?

UPDATE 
  Table1, 
  Table2
SET 
  Table1.LastName=\'DR. XXXXXX\', 
  Table2.WApr         


        
9条回答
  •  伪装坚强ぢ
    2020-11-22 05:58

    This works for MySQL and is really just an implicit transaction but it should go something like this:

    UPDATE Table1 t1, Table2 t2 SET 
    t2.field = t2.field+2,
    t1.field = t1.field+2
    
    WHERE t1.id = t2.foreign_id and t2.id = '123414'
    

    if you are doing updates to multi tables that require multi statements… which is likely possible if you update one, then another based on other conditions… you should use a transaction. 

提交回复
热议问题