Jeff Jeff - 1 year ago 75
SQL Question

Create Loop to use value from another table to perform deletions

I am trying to figure out a way to create a loop that will grab an id from a column of a different table and use that id to perform deletions.

represents a different ID from another table. How can I create a loop to read through each ID and delete rows from other tables based off that ID?

Declare @ID int
Set @ID =

Delete from where id=@id
Delete from dbo.searchfolder where id=@id
Delete from dbo.application where id =@id

Answer Source

You dont need a loop ,just join will work,you can implement same logic for all tables

Delete s
from s
dbo.content c

You also can use exists,which may be faster in some scenarios depending on data density

delete from t2 where exists(select 1 from dbo.t1 where
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download