Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to update multiple columns from multiple tables in a single UPDATE Query...
Just want to do like below query...
SQL
UPDATE Table1, Table2
SET Table1.Column1 = 'one' 
,Table2.Column2 = 'two'
FROM Table1 T1, Table2 T2
WHERE T1.id = T2.id
and T1.id = 'id1'



Does Sql Server 2008 provide any mechanism to do so?

If Sql Server 2008 provide such nice approach, please share some links with me!

Thanks!
Posted
Updated 8-Mar-13 5:28am
v2
Comments
ZurdoDev 8-Mar-13 9:31am    
Silly question, but have you tried it?

Well, in short - not possible.The same restriction for updateable views:
SQL
Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.



Approach that I believe is optimal: use transactions, possibly wrap your business logic into stored procedures. Code skeleton might look like below:

SQL
begin tran

<TSQL statement for table 1>
if @@error != 0 goto error

<TSQL statement for table 2>
if @@error != 0 goto error

commit tran
goto ok

error:

<error_handler>
rollback tran

ok:
 
Share this answer
 
v2
try using foreign key "on update cascade".

see if id's or by any other means primary key- foreign key constraint can be applied
 
Share this answer
 
Comments
Member 8090436 11-Mar-13 10:10am    
Thanks,
Your provided approached helpful only while we need to change Parent column and want to update child column value when they have Primary-foreign key relationship.
But in my scenario assume both belongs to different tables and also different columns.
If you have idea suggest me one or two!
Thanks!

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