How to Delete using INNER JOIN with SQL Server?

后端 未结 16 1724
刺人心
刺人心 2020-11-22 10:50

I want to delete using INNER JOIN in SQL Server 2008.

But I get this error:

Msg 156, Level 15, State 1, Line 15<

相关标签:
16条回答
  • 2020-11-22 11:21

    In SQL Server Management Studio I can easily create a SELECT query.

    SELECT Contact.Naam_Contactpersoon, Bedrijf.BedrijfsNaam, Bedrijf.Adres, Bedrijf.Postcode
    FROM Contact
    INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
    

    I can execute it, and all my contacts are shown.

    Now change the SELECT to a DELETE:

    DELETE Contact
    FROM Contact
    INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
    

    All the records you saw in the SELECT statement will be removed.

    You may even create a more difficult inner join with he same procedure, for example:

    DELETE FROM Contact
    INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
    INNER JOIN LoginBedrijf ON Bedrijf.IDLoginBedrijf = LoginBedrijf.IDLoginBedrijf
    
    0 讨论(0)
  • 2020-11-22 11:28

    You don't specify the tables for Company and Date, you might want to fix that.

    Standard SQL using MERGE:

    MERGE WorkRecord2 T
       USING Employee S
          ON T.EmployeeRun = S.EmployeeNo
             AND Company = '1'
             AND Date = '2013-05-06'
    WHEN MATCHED THEN DELETE;
    

    The answer from @Devart is also Standard SQL though incomplete, should look more like this:

    DELETE 
      FROM WorkRecord2
      WHERE EXISTS ( SELECT *
                       FROM Employee S
                      WHERE S.EmployeeNo = WorkRecord2.EmployeeRun
                            AND Company = '1'
                            AND Date = '2013-05-06' );
    

    The important thing to note about the above is it is clear the delete is targeting a single table, as enforced in the second example by requiring a scalar subquery.

    For me the various proprietary syntax answers are harder to read and understand. I guess the mindset for is best described in the answer by @frans eilering i.e. the person writing the code doesn't necessarily care about the person who will read and maintain the code.

    0 讨论(0)
  • 2020-11-22 11:28

    You could even do a sub-query. Like this code bellow:

    DELETE FROM users WHERE id IN(
        SELECT user_id FROM Employee WHERE Company = '1' AND Date = '2013-05-06'
    )
    
    0 讨论(0)
  • 2020-11-22 11:33

    Try this:

    DELETE FROM WorkRecord2 
           FROM Employee 
    Where EmployeeRun=EmployeeNo
          And Company = '1' 
          AND Date = '2013-05-06'
    
    0 讨论(0)
  • 2020-11-22 11:38

    This version should works

    DELETE WorkRecord2
    FROM WorkRecord2 
    INNER JOIN Employee ON EmployeeRun=EmployeeNo
    Where Company = '1' AND Date = '2013-05-06'
    
    0 讨论(0)
  • 2020-11-22 11:41

    Here's what I currently use for deleting or even, updating:

    DELETE           w
    FROM             WorkRecord2   w,
                     Employee      e
    WHERE            w.EmployeeRun = e.EmployeeNo
                 AND w.Company = '1' 
                 AND w.Date = '2013-05-06'
    
    0 讨论(0)
提交回复
热议问题