Most efficient method to detect column change in MS SQL Server

后端 未结 5 1733
情深已故
情深已故 2020-12-05 05:10

Our system runs on SQL Server 2000, and we are in the process of preparing for an upgrade to SQL Server 2008. We have a lot of trigger code where we need to detect a change

5条回答
  •  时光说笑
    2020-12-05 05:15

    I think you may want to investigate using the EXCEPT operator. It is a set based operator that can weed out the rows that have not changed. The nice thing is that considers null values as equal as it looks for rows in the first set listed before the EXCEPT operator and not in the second Listed After the EXCEPT

    WITH ChangedData AS (
    SELECT d.Table_ID , d.Col1 FROM deleted d
    EXCEPT 
    SELECT i.Table_ID , i.Col1  FROM inserted i
    )
    /*Do Something with the ChangedData */
    

    This handles the issue of columns that allow Nulls without the use of ISNULL() in the trigger and only returns the ids of the rows with changes to col1 for a nice set based approach to detecting changes. I haven't tested the approach but it may well be worth your time. I think EXCEPT was introduced with SQL Server 2005.

提交回复
热议问题