Results 1 to 6 of 6

Thread: Delete with join query

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Delete with join query

    I have a query I use to return rows from two tables with kriteries. But how can I convert this query to a delete query?
    I wanna delete the result of this query on both tables.
    I have try to replce SELECT tblCust.*, tblCustDesc.* with Delete tblCust, tblCustDesc without any luck.

    Code:
    SELECT tblCust.*, tblCustDesc.*
    FROM tblCust INNER JOIN tblCustDesc ON tblCust.ID = tblCustDesc.Order_ID 
    WHERE tblCustDesc.State_ID In (SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK') 
    AND tblCust.State_ID In (SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK') 
    AND tblCust.StartedBy Is Null

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Delete with join query

    You need to delete each table individually. You can't delete two tables in one statement.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Delete with join query

    Ok, thanks for the answer. I believe it was possible.

    So, If I should do this this individually, I must start with table tblCustDesc. How should the queries look lik then? Like this? Or do you have any other better solutions?

    Code:
    DELETE FROM tblCustDesc 
        WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK') 
        And Order_ID  IN  ( SELECT tblCust.ID FROM tblCust WHERE tblCust.StartedBy Is Null) 
        And Order_ID  IN  ( SELECT tblCust.ID FROM tblCust WHERE tblCust.state_ID = 
                          ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description = 'OK');
        
        'Execute the query
       
    
    DELETE FROM tblCust
        WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
        And ID Not IN ( SELECT tblCustDesc.Order_ID FROM tblCustDesc WHERE state_ID IN
                      ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description <> 'OK')
        And tblCust.StartedBy Is Null;
    
    'Execute the query
    Last edited by Pirre001; Feb 21st, 2009 at 09:19 AM.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Delete with join query

    Last one is missing a close ) before the last And
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Delete with join query

    OK, so this is the right way to do the delete query?
    Code:
    DELETE FROM tblCustDesc 
        WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK') 
        And Order_ID  IN  ( SELECT tblCust.ID FROM tblCust WHERE tblCust.StartedBy Is Null) 
        And Order_ID  IN  ( SELECT tblCust.ID FROM tblCust WHERE tblCust.state_ID = 
                          ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description = 'OK'));
        
        'Execute the query
       
    
    DELETE FROM tblCust
        WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
        And ID Not IN ( SELECT tblCustDesc.Order_ID FROM tblCustDesc WHERE state_ID IN
                      ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description <> 'OK'))
        And tblCust.StartedBy Is Null;
    
    'Execute the query

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Delete with join query

    That look good
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

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



Click Here to Expand Forum to Full Width