简体   繁体   中英

SQL Server compare values of two rows of same table and get not matching column names

I need to compare two rows ( INSERTED & DELETED ) of the same table and compare values. And then I need to get the not matching columns. Inside a trigger.

Here what I've tried so far but I 've no idea of how to compare these two rows and get column names which contains different values .

SELECT * 
FROM inserted i
INNER JOIN deleted d ON d.PurchasingDocItemNo = i.PurchasingDocItemNo 
                     AND d.PurchasingDocNo = i.PurchasingDocNo
                     AND d.ReferenceDocumentNo = i.ReferenceDocumentNo
                     AND d.ProductNo = i.ProductNo

Change your select to :

SELECT
-- For each column you're checking.  Also account for ISNull if you need to
CASE WHEN I.Col1 = D.Col1 THEN 0 ELSE 1 END AS Col1Changed.......
FROM INSERTED I
INNER JOIN DELETED D on (I.<PK> = D.<PK>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM