Sunday, February 19, 2012

Delete Records when you have a primary key of two Columns

Hi everyone.

I have two tables: the catalog table and the detail table.
The two tables are joined by a two-columns key.

I want to make a Delete sentence for delete all the rows in the Catalog table that aren't in the detail table, in SQL Server.

The only problem is that the key is composed by two colums.

If the Key was maded of one column, that would be easy, like this:

DELETE FROM CATALOG
WHERE CATALOGKEY NOT IN (SELECT CATALOGKEY FROM DETAIL)

But it is possible to make a delete sentence if the key has two columns ?

ThanksBut it is possible to make a delete sentence if the key has two columns ?But of course :)

DELETE
FROM CATALOG
WHERE NOT EXISTS
(SELECT *
FROM DETAIL
WHERE DETAIL.CATALOGKEY = CATALOG.CATALOGKEY
AND DETAIL.FIELD2= CATALOG.FIELD2)

hth|||I get a sense that something else is afoot...

Read the sticky at the top of the forum and post what it asks for

No comments:

Post a Comment