How to Delete using INNER JOIN with SQL Server?

后端 未结 16 2079
刺人心
刺人心 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
    

提交回复
热议问题