Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Can we update two table in a single query

Author  Topic 

boreddy
Posting Yak Master

172 Posts

Posted - 2008-12-23 : 02:11:27
in my databse i have 3 related tables
i want to update two table columns by joining the three tables
is it possible
thank you

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-23 : 03:38:18
yes
use trigger

try like this or post ur tables structures

Update t
set col1= value,
col2 = value2
from table t
inner join table1 on t.id = t1.id
inner join table2 on t.id = t2.id

Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-23 : 03:45:47
User triggers, It will be more useful to u..

CREATE TRIGGER Trig_StudentDetails
ON
table1
For Update AS
BEGIN
--Update Statement For table1
Update tbl_StudentDetails set StdName='jammmu' where StdId = 9

END

--Update Statement for Table2
Update tbl_StudentDetails1
set StdName='Jaggu' where StdId= 11

I Struggle For Excellence
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-25 : 01:52:30
or what you can do is to create a view which combines the tables and update the tables through them. But there should not be any complex statements in view like aggregations,.. for them to become updatable. Else you need trigger.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-25 : 01:53:51
see this too
http://weblogs.sqlteam.com/brettk/archive/2008/04/03/60560.aspx
Go to Top of Page
   

- Advertisement -