简体   繁体   English

SQL Server 比较同一表的两行的值并得到不匹配的列名

[英]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.我需要比较同一个表的两行( INSERTEDDELETED )并比较值。 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>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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