Created
May 16, 2011 19:07
-
-
Save danesparza/975088 to your computer and use it in GitHub Desktop.
Delete data from multiple tables at once - SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin transaction; | |
declare @deletedIds table ( id int ); | |
delete t1 | |
/* Notice this next line is using the 'deleted' pseudo table: */ | |
output deleted.id into @deletedIds; | |
from table1 t1 | |
join table2 t2 | |
on t2.id = t1.id | |
join table3 t3 | |
on t3.id = t2.id; | |
delete t2 | |
from table2 t2 | |
join @deletedIds d | |
on d.id = t2.id; | |
delete t3 | |
from table3 t3 ... | |
commit transaction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment