Removing Equivalent Data from Two Tables

I need to execute the following pseudo logic in a SQL Server 2012 procedure based on a table variable and a table declared as such:

DECLARE @tmp TABLE
    (
    ID int IDENTITY(1,1),
    UserID int NOT NULL,
    SgsID int NOT NULL
    )

CREATE TABLE #Table1
    (
    ID int IDENTITY(1,1),
    UserID int NOT NULL,
    SgsID int NOT NULL
    )
  • For each row of data in a table variable @tmp
  • Remove rows from Table1where combinations UserID/SgsIDmatch UserID/SgsIDinTable1
  • Delete those UserID/SgsIDcombinations from @tmpthat were removed fromTable1

I have studied various approaches, such as using OUTPUT INTOand INTERSECT, but I canโ€™t write a query that removes over two tables (in fact, I donโ€™t think it is even possible).

I followed the steps above using the following code, however I was wondering if any T-SQL pro could offer a more concise / efficient approach?

SQLFiddle -

CREATE TABLE #Table1
    (
    ID int IDENTITY(1,1),
    UserID int NOT NULL,
    SgsID int NOT NULL
    )

INSERT INTO #Table1 (UserID, SgsID) VALUES (5, 99)
INSERT INTO #Table1 (UserID, SgsID) VALUES (10, 89)
INSERT INTO #Table1 (UserID, SgsID) VALUES (150, 79)
INSERT INTO #Table1 (UserID, SgsID) VALUES (200, 69)
INSERT INTO #Table1 (UserID, SgsID) VALUES (250, 59)
SELECT * FROM #Table1 

DECLARE @tmp TABLE
    (
    ID int IDENTITY(1,1),
    UserID int NOT NULL,
    SgsID int NOT NULL
    )

INSERT INTO @tmp (UserID, SgsID) VALUES (150, 79)
INSERT INTO @tmp (UserID, SgsID) VALUES (200, 69)
INSERT INTO @tmp (UserID, SgsID) VALUES (250, 59)
INSERT INTO @tmp (UserID, SgsID) VALUES (999, 49)
SELECT * FROM @tmp

DECLARE @tbl_commonRows TABLE (UserID int, SgsID int)
INSERT INTO @tbl_commonRows 
    (
    UserID,
    SgsID
    ) 
SELECT 
    UserID,
    SgsID 
FROM
    #Table1
INTERSECT 
SELECT
    UserID,
    SgsID
FROM
    @tmp 

DELETE FROM 
    #Table1 
WHERE 
    (ID IN (
        SELECT 
            ID 
        FROM 
            #Table1 t1 INNER JOIN
            @tbl_commonRows c ON c.UserID = t1.UserID AND c.SgsID = t1.SgsID))

DELETE FROM
    @tmp
WHERE
    (ID IN (
        SELECT
            ID
        FROM
            @tmp t2 INNER JOIN
            @tbl_commonrows c ON c.UserID = t2.UserID AND c.SgsID = t2.SgsID))

SELECT * FROM #Table1 
SELECT * FROM @tmp
DROP TABLE #Table1
+5
3
+1

:

DECLARE @tmp_ids TABLE (
    id1 INT,
    id2 INT
)

INSERT INTO @tmp_ids (id1, id2)
SELECT 
    t1.id,
    t2.id
FROM Table1 t1
INNER JOIN tmp t2
    on (t1.UserID = t2.UserID AND t1.SgsID = t2.SgsID)

DELETE FROM Table1
WHERE id IN (SELECT id1 FROM @tmp_ids)

DELETE FROM tmp
WHERE id IN (SELECT id2 FROM @tmp_ids)

- tmp Table1

+4

You can take advantage of the fact that the OUTPUT command can accept more than the INSERTED and DELETED columns for deletion (but does not insert, unfortunately):

    DECLARE @output TABLE (id int) 

    DELETE FROM tbl
    OUTPUT tmp.ID INTO @output(id)
    FROM #Table1 tbl
    JOIN @tmp tmp 
        ON tbl.UserID = tmp.UserID 
        AND tbl.SgsID = tmp.SgsID

    DELETE FROM tmp
    FROM @tmp tmp
    JOIN @Output outp ON tmp.id = outp.id   
+2
source

All Articles