2018-03-23 sql server 中有几种 做“某列数据是否已经有updated”的判断语句?

sql server 中有几种 做“某列数据是否已经有updated”的判断语句?


微软的官方答案:

Microsoft docs references:

UPDATE()

COLUMNS_UPDATED


2018-03-23 sql server 中有几种 做“某列数据是否已经有updated”的判断语句?_第1张图片

最好的解释见 下面

网页转载: 

https://dev.to/ravenous_baboon/checking-if-the-column-was-updated-inside-sqlserver-update-trigger


There are three ways one can check if a column was updated inside a trigger:

Check for the value of UPDATE(Column_Name)

Check for the value of COLUMNS_UPDATED() & integer mask for the column updated (also works for more than one column)

Check if a column appears in an inserted table - IF EXISTS(SELECT Column_Name FROM inserted)

However these three do not work in the same way.

Example:

We have a table dbo.Customers and we want to check for updates on its first column, LastName. We will create a trigger which will incorporate all three of the methods. It will print a message for each condition that was met:

2018-03-23 sql server 中有几种 做“某列数据是否已经有updated”的判断语句?_第2张图片

Conclusion: Checks for UPDATE() and COLUMNS_UPDATED () return true if a column we check for was in an update list, regardless of an actual data update. For the IF EXISTS(SELECT Column_Name FROM inserted) check we need some actual data to be updated.

Microsoft docs references:

UPDATE()

COLUMNS_UPDATED

你可能感兴趣的:(2018-03-23 sql server 中有几种 做“某列数据是否已经有updated”的判断语句?)