Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL After Update Trigger check for update on multiple columns in one IF UPDATE

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.

like image 260
J3FFK Avatar asked Aug 01 '14 10:08

J3FFK


People also ask

How do I update multiple columns in SQL at the same time?

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.

How can I tell which columns were changed in a SQL Server update?

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().

How do you execute a trigger only when a specific column is 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.

Can we use update command in 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.


2 Answers

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.

like image 125
Gordon Linoff Avatar answered Nov 08 '22 13:11

Gordon Linoff


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.

like image 34
Damien_The_Unbeliever Avatar answered Nov 08 '22 12:11

Damien_The_Unbeliever