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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Delete in Loop

Author  Topic 

stroek
Starting Member

18 Posts

Posted - 2008-08-11 : 07:22:14
Hello All

I can't find a solution for next problem.

I want to loop through a select (ex: select ID from TableA)

And delete records from an other table using de selected records

delete from TableB where relationID = TableA.ID

regards

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-11 : 07:47:48
[code]
delete B
from TableB B inner join TableA A
on B.relationID = A.ID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

stroek
Starting Member

18 Posts

Posted - 2008-08-11 : 07:59:18
tks,

I had an other solution,

Delete from TableB where relationID in (select ID from TableA)
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-11 : 10:02:59
Yes, that'll work but Khtan's way is quicker.

-------------
Charlie
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-11 : 10:08:29
If you want to remove a large number of records, you might want to delete the records in batches instead.
Otherwise the LOG file will grow.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -