Using variable for UPDATE() the checker if the column is Updated or not

前端 未结 2 493
温柔的废话
温柔的废话 2021-01-29 06:03

Is it possible to use a variable in UPDATE() which check if a column is updated or not?

here is my sample code:

DECLARE @ColumnCount int
DECLARE @ColumnC         


        
2条回答
  •  误落风尘
    2021-01-29 06:29

    to check for column is actually updated in after update trigger you can use deleted and inserted special tables msdn in which trigger code can find old and new versions of rows of updating table

    remember that it is possible to find there more than 1 row in each table. for example you execute update table1 set col1 = value1 where some_condition in trigger number of rows affects by some_condition, will be presented in inserted and deleted tables

    to find out in trigger in what rows some column is actual updating, you can join inserted and deleted tables on some primary key and compare values of this column

    the update() function takes as parameter column name not a sting variable with name as value

    for example using update() in triggers check this update(some_column) is true when some_column present in update set statement, even value of column not changing.

    it seems like you can not write universal trigger to count number of columns updated, if you need that count you need to manually write trigger for each table with somethig like this

    declare @count int = 0
    if update(col1) set @count = @count + 1
    if update(col2) set @count = @count + 1
    ...
    

提交回复
热议问题