The basic syntax for an After Update trigger in TSQL is:
CREATE TRIGGER [dbo].[triggerName]
ON [dbo].[Table]
AFTER UPDATE, INSERT --trigger when Update or Insert in table
AS BEGIN
SET NOCOUNT ON;
IF UPDATE (ColumnA) -- if ColumnA updates start
begin
UPDATE Table
SET ColumnC = ColumnA + ColumnB -- recalculate Column C
end
IF UPDATE (ColumnB) -- if ColumnB updates start
begin
UPDATE Table
SET ColumnC = ColumnA + ColumnB -- recalculate Column C
end
END
Now above could work I guess, but is it possible to combine both IF UPDATES in one?: Something like this:
IF UPDATE (ColumnA) OR (ColumnB) -- if ColumnA or ColumnB updates start
begin
UPDATE Table
SET ColumnC = ColumnA + ColumnB -- recalculate Column C
end
You could use a computed column of course, but out of curiosity I'd like to know if you can check for updates on multiple columns at once in a trigger and then do the after update trigger modification.
We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.
Solution. 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().
In SQL Server, you can create DML triggers that execute code only when a specific column is updated. The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated. You can do this by using the UPDATE() function inside your trigger.
AFTER UPDATE Trigger is a kind of trigger in SQL that will be automatically fired once the specified update statement is executed. It can be used for creating audit and log files which keep details of last update operations on a particular table.
This is a bit long for a comment.
UPDATE()
is a function used in a trigger. There is not specific syntax as if UPDATE()
. As far as I know, triggers allow you to combine multiple conditions in an if
statement.
But, even more to the point, read the documentation on UPDATE()
. The very example used in the documentation is:
IF ( UPDATE (StateProvinceID) OR UPDATE (PostalCode) )
BEGIN
RAISERROR (50009, 16, 10)
END;
So, use the correct syntax for yours and you will be fine:
IF UPDATE(ColumnA) OR UPDATE(ColumnB) -- if ColumnA or ColumnB updates start
begin
UPDATE Table
SET ColumnC = ColumnA + ColumnB -- recalculate Column C
end
However, I would suggest that you use a computed column instead.
You can use COLUMNS_UPDATED
:
Returns a
varbinary
bit pattern that indicates the columns in a table or view that were inserted or updated.
And just check for any of the bits you're interested in being set using bitwise operators:
IF (COLUMNS_UPDATED() & CAST(0x0A as int)) != 0
begin
--column 2 or 4 was updated
end
Of course, this does then depend on the column order, which is usually a bad thing to depend upon, and it also won't blow up if the column order is later changed.
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