Thursday, March 22, 2012

Delete

Hello,

I have 3 tables:

[Article] > ArticleId, ...
[Category] > CategoryId, ...
[CategoriesInArticles] > ArticleId, CategoryId

Given an @.ArticleId, I need to:

1. Delete the record in [Article] with the given @.ArticleId

2. Delete all records in [CategoriesInArticles] with the given @.ArticleId

3. Delete the records in [Categories] which CategoryId was deleted in (2) BUT only if the CategoryId
is not associated with other articles in [CategoriesInArticles].

How can I do this?

Thanks,
Miguel

delete from CategoriesInArticles where ArticleID = @.ArticleID

delete from Categories where CategoryID not in (select distinct CategoryID from CategoriesInArticles)

delete from Article where ArticleID = @.ArticleID

That is the easiest way...

No comments:

Post a Comment