Skip to content

Instantly share code, notes, and snippets.

@danesparza
Created May 16, 2011 19:07
Show Gist options
  • Save danesparza/975088 to your computer and use it in GitHub Desktop.
Save danesparza/975088 to your computer and use it in GitHub Desktop.
Delete data from multiple tables at once - SQL
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