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)
 Loop through table

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-08-06 : 15:43:38
Trying to avoid a cursor to do this, so posting the question here.

For each PK id in tbl1, a field in tbl2 needs to be checked.
If the PK in tbl1 does NOT exist in tbl2
Then delete that record in tbl1

Like I said, could use a cursor for this but I know there's a better way.

Thanks,

Zath

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-06 : 16:03:06
[code]delete A from tbl1 A LEFT JOIN tbl2 B on A.PK = B.PK where B.PK is NULL[/code]
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-08-06 : 16:37:04
delete A from tbl1 A where not exists(select * from tbl2 where PK=A.PK)
Go to Top of Page
   

- Advertisement -