Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can we join two tables and have an update command which updates both the tables?
Thanks
Posted

1 solution

You can't update multiple tables in one statement. Instead of that you can use a transaction to make sure that two Update statements are treated atomically.
Eg:
SQL
DECLARE @MyTable TABLE (id int);
BEGIN TRANSACTION
UPDATE Table1
SET Table1.Name = 'Test'
OUTPUT INSERTED.id INTO @MyTable
WHERE T1.field = 'Your Value';
UPDATE Table2
SET Table2.YourColumnName = 'Your Value'
FROM Table2
JOIN @MyTable m on m.id = Table2.id;

COMMIT;



--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900