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 2005 Forums
 Transact-SQL (2005)
 DELETE

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-03-21 : 11:14:58
Hello,

I have two tables with their columns as follows:
LabelsInDocs [LabelId]PK, [DocsId]PK
Labels [LabelId]PK, [LabelName]
Docs [DocId]PK, [DocUrl]

When I delete all records in LabelsInDocs given a DocId I need to:
> Delete all records in Labels which LabelId is not present anymore in LabelsInDocs.
Basically I need to delete all Labels which are not associated with any Doc anymore.

How can I do this?

Thanks,
Miguel

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 11:24:38
[code]Delete l
from Labels l Left join LabelsInDocs d
on d.LabelId = l.LabelId
where l.LabelId is NULL[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-03-21 : 13:48:09
Harsh,

If I use cascade delete, which means that if I delete a label or a doc all records in LabelsInDocs with given LabelId or DocId will be deleted, how can I "order" to delete all unused labels in Labels?

Thanks,
Miguel
Go to Top of Page
   

- Advertisement -