Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Location
    Guadalajara
    Posts
    9

    Unanswered: 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 ?


    Thanks

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by fnajar
    But it is possible to make a delete sentence if the key has two columns ?
    But of course

    Code:
    DELETE 
    FROM CATALOG
    WHERE NOT EXISTS
    (SELECT * 
    FROM DETAIL 
    WHERE DETAIL.CATALOGKEY = CATALOG.CATALOGKEY 
    AND DETAIL.FIELD2= CATALOG.FIELD2)
    hth
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I get a sense that something else is afoot...

    Read the sticky at the top of the forum and post what it asks for
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •