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

后端 未结 9 1399
轻奢々
轻奢々 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 06:13

    It is as simple as this query shown below.

    UPDATE 
      Table1 T1 join Table2 T2 on T1.id = T2.id
    SET 
      T1.LastName='DR. XXXXXX', 
      T2.WAprrs='start,stop'
    WHERE 
      T1.id = '010008'
    
    0 讨论(0)
  • 2020-11-22 06:17

    Sorry, afaik, you cannot do that. To update attributes in two different tables, you will need to execute two separate statements. But they can be in a batch ( a set of SQL sent to the server in one round trip)

    0 讨论(0)
  • 2020-11-22 06:18

    You can't update multiple tables in one statement, however, you can use a transaction to make sure that two UPDATE statements are treated atomically. You can also batch them to avoid a round trip.

    BEGIN TRANSACTION;
    
    UPDATE Table1
      SET Table1.LastName = 'DR. XXXXXX' 
    FROM Table1 T1, Table2 T2
    WHERE T1.id = T2.id
    and T1.id = '011008';
    
    UPDATE Table2
    SET Table2.WAprrs = 'start,stop'
    FROM Table1 T1, Table2 T2
    WHERE T1.id = T2.id
    and T1.id = '011008';
    
    COMMIT;
    
    0 讨论(0)
提交回复
热议问题