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
A few things: You may want to handle the first time through the loop because the name would not have changed the first time through. You may want to handle when @ColumnName is null because that would mean your query did not return a row although your query below should always return a value.
DECLARE @ColumnCount INT
DECLARE @ColumnCounter INT
DECLARE @ColumnName NVARCHAR(max)
DECLARE @temp varchar(max)
SET @ColumnCounter = 0
SELECT @ColumnCount = Count(c.column_name)
FROM information_schema.columns c
WHERE c.table_name = 'Province'
WHILE @ColumnCount >= @ColumnCounter
BEGIN
SET @ColumnName = NULL
SELECT @ColumnName = c.column_name
FROM information_schema.columns c
WHERE c.table_name = 'Province'
AND c.ordinal_position = @ColumnCounter
IF ( @ColumnName != @temp )
BEGIN
--do something
END
SET @temp = @ColumnName
SET @ColumnCounter = @ColumnCounter + 1
END
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
...