Home > BI, Code, Tips > T-SQL Delete Join

T-SQL Delete Join

Here’s are two quick ways to Delete data in one table based on another table.  This works perfectly in an ETL job where you have a staging table that indicate whether to Insert, Update, or Delete the data in the other table.

The first one uses an inner join, the second uses a simple EXISTS clause.

DELETE b

  FROM [dbo].[TABLE_1] b

 INNER JOIN [dbo].[TABLE_2] w

    ON b.[ROW_ID] = w.[ROW_ID]

 WHERE w.[CTRL_CD] = 'D'

 

 

DELETE b

  FROM [dbo].[TABLE_1] b

 WHERE EXISTS (

            SELECT 1

          FROM [dbo].[TABLE_2] w

         WHERE b.[ROW_ID] = w.[ROW_ID]

           AND w.[CTRL_CD] = 'D')

 

VN:F [1.9.6_1107]

Rating: 4.8/5 (4 votes cast)
VN:F [1.9.6_1107]
Rating: +1 (from 1 vote)
T-SQL Delete Join, 4.8 out of 5 based on 4 ratings
Categories: BI, Code, Tips Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.