Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2004
    Posts
    3

    Exclamation Unanswered: Delete from 2 Tables with 1 SQL Query

    I have 2 tables that are joined together by a primary key (Order Number). Can I use one SQL query to delete from both of the tables. One table contains the order information from a client (Order Number, Customer Name etc). The other table has order information (Order Number, Item Number, Quantity Ordered etc.)

    I need one statement that will allow me to remove the items from both tables. Can this be done.

    Thanks in Advance

    Wes

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd do two separate DELETE operations within a single transaction.

    -PatP

  3. #3
    Join Date
    Mar 2004
    Posts
    3
    Agreed, but is there any way to use a single transaction?

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    CASCADE...are the tables related?

    I never use it though.....
    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.

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Yes, I intended for you to only use one transaction. Something like:
    PHP Code:
    BEGIN TRANSACTION

    DELETE FROM dbo
    .table1 WHERE order_number = @x

    DELETE FROM dbo
    .table2 WHERE order_number = @x

    COMMIT TRANSACTION 
    with error checking and other code added to taste.

    -PatP

  6. #6
    Join Date
    Mar 2004
    Posts
    3
    Thanks Guys for all the help!

  7. #7
    Join Date
    Dec 2003
    Posts
    31
    you can try also a delete trigger

Posting Permissions

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