469,126 Members | 1,258 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,126 developers. It's quick & easy.

How to delete records from 2 tables


how to delete records which has same id from 2 tables.

In oracle

we can use

delete from t1,t2 where t1.id=1 and t1.id=t2.id

how to do this in sqlserver.
Mar 8 '07 #1
1 2344
839 Expert 512MB
If want to delete records in 2 tables at the same time (with 1 statement) you must create a trigger like
Expand|Select|Wrap|Line Numbers
  1. if exists(select name from sysobjects where name = 'tg1' and type = 'TR')
  2. drop trigger tg1
  3. go
  4. create trigger tg1 on t1 for Delete as
  5. delete t2 from 
  6. t2 inner join deleted on deleted.id=t2.id
  7. go
Then, just delete the t1 record
Expand|Select|Wrap|Line Numbers
  1. delete from t1 where id = 1
Mar 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Job Lot | last post: by
3 posts views Thread by Tim Marshall | last post: by
3 posts views Thread by John Rivers | last post: by
2 posts views Thread by nekiv90 | last post: by
3 posts views Thread by Phil Stanton | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.