I have AFTER UPDATE trigger on table.
I need to get the name of changing colomn and it's old and new values.
To do statement UPDATE(column_name)
with each column in code - bad solution. But I can't get all table colomns names via query
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'smth'
and in cursor dynamically get UPDATE(@column_name)
value.
At same time when I try to use COLUMNS_UPDATED()
function when I update only one column in table, I have results (value converted to int):
64 (Updated column with ORDINAL_POSITION = 31)
32 (Updated column with ORDINAL_POSITION = 30)
8 (Updated column with ORDINAL_POSITION = 29)
4 (Updated column with ORDINAL_POSITION = 28)
2 (Updated column with ORDINAL_POSITION = 27)
1 (Updated column with ORDINAL_POSITION = 26)
32768 (Updated column with ORDINAL_POSITION = 25)
I think it is very strange and ask your help.
AFTER UPDATE Trigger in SQL is a stored procedure on a database table that gets invoked or triggered automatically after an UPDATE operation gets successfully executed on the specified table. For uninitiated, the UPDATE statement is used to modify data in existing rows of a data table.
Using a SQL Server trigger to check if a column is updated, there are two ways this can be done; one is to use the function update(<col name>) and the other is to use columns_updated().
The following Trigger is fetching the CustomerId of the updated record. In order to find which column is updated, you will need to use UPDATE function and pass the Column name of the Table to it. The UPDATE function will return TRUE for a Column if its value was updated else it will return false.
It facilitates you to change the trigger definition without using a DROP TRIGGER statement. trigger_name: It specifies the name of the trigger that you want to create. AFTER INSERT or UPDATE or DELETE: It specifies that the trigger will be fired after the INSERT or UPDATE or DELETE operation is executed.
Whether it is strange or not, this is at least documented:
Caution
In SQL Server 2008, the ORDINAL_POSITION column of the INFORMATION_SCHEMA.COLUMNS view is not compatible with the bit pattern of columns returned by COLUMNS_UPDATED. To obtain a bit pattern compatible with COLUMNS_UPDATED, reference the ColumnID property of the COLUMNPROPERTY system function when you query the INFORMATION_SCHEMA.COLUMNS view, as shown in the following example.
SELECT TABLE_NAME, COLUMN_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS COLUMN_ID FROM AdventureWorks2008R2.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Contact';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With