Robin clave
Robin clave

Reputation: 638

sql server cursor comparison

i have 2 cursor in which cursor 1 has select * from table 1 and cursor 2 has select * from table 2. I need to compare 2 cursors and if the fetched row in the cursor 1 is not equal to fetched row of cursor 2, then i wants to delete that fetched row from table 2. Please help me how to do this?

Upvotes: 0

Views: 473

Answers (2)

Anthony Faull
Anthony Faull

Reputation: 17957

You can use EXCEPT to identify the changed rows.

;WITH DirtyRows AS
(
    SELECT * FROM [Table 1]
    EXCEPT
    SELECT * FROM [Table 2]
)
DELETE [Table 2]
WHERE EXISTS
(
    SELECT * FROM DirtyRows
    WHERE DirtyRows.Id = [Table 2].Id
)

Upvotes: 1

Lamak
Lamak

Reputation: 70638

Why would you want to do that with cursors?, If I understood you correctly, you can just do:

DELETE B
FROM table1 A
INNER JOIN table2 B
ON A.Id = B.Id
WHERE A.column1 <> B.column1 OR A.column2 <> B.Column2 ....

Or something like that.

Upvotes: 1

Related Questions