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 2008 Forums
 Transact-SQL (2008)
 Delete query

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-05-10 : 12:20:02
Can you please help, I want to delete all rows from tab_child, if parent does'nt exist in tab_parent:

delete Tab_Child from Tab_child t1 join TAB_parent su on (t1.ModuleID=su.suid)


Thank you very much for the helpful info.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-10 : 12:49:45
delete c
from Tab_child c
where not exists (select * from TAB_parent p where c.ModuleID = p.suid)

You can use the join approach too, but it'll need to be LEFT JOIN with NULL check.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -