SQL Server: Delete a record with condition on multiple columns of another table

If you want to delete a record from a table where the relation with another table is on multiple columns or you want to check it for some conditions on multiple columns of another table then with a simple join your job can be done.

Example:


DELETE Table1

FROM  Table1

INNER JOIN  Table2 ON  Table1.Column1=Table2.Column1 AND Table1.Column2=Table2.Column2

Drop a comment in case you have any question or suggestion.

Thanks !

3 thoughts on “SQL Server: Delete a record with condition on multiple columns of another table

  1. Thank you! I had to convert PostgreSQL syntax into MSSQL syntax.

    The PostgreSQL syntax was:

    DELETE FROM Table1 USING Table2, Table3 WHERE Table1.id = Table2.id AND Table2.id = Table3.id AND Table3.id = @id

    Which using your solution I was able to convert to MSSQL syntax:

    DELETE Table1 FROM Table1
    INNER JOIN Table2 ON Table1.id = Table2.id
    INNER JOIN Table3 ON Table2.id = Table3.id
    WHERE Table3.id=@id

    • I am really happy to know that the post was useful to you. Thanks for spending your valuable time to write a feedback. Again I too learned a new thing about PostgreSQL from you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s