简体   繁体   中英

Check if field value updated on sql server trigger

How I can check field value updated in this trigger:

ALTER TRIGGER dbo.OrderApprovedSms 
   ON  dbo.[Order]
   AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    /*How can check approve date is updated*/
    IF (/*ApproveDate is updated*/)
    BEGIN
       INSERT INTO office.SmsSendBuffer
               ( Number ,
                 Body 
               )
       SELECT 'xxxxxx','ORDER APPROVED!'
    END

END

It would be something like:

ALTER TRIGGER dbo.OrderApprovedSms 
   ON  dbo.[Order]
   AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    INSERT INTO office.SmsSendBuffer
         ( Number ,
             Body 
         )
    SELECT 'xxxxxx','ORDER APPROVED!' --No columns from inserted or deleted?
    FROM inserted i INNER JOIN deleted d
        ON i.<primary key column 1> = d.<primary key column 1> AND
           i.<primary key column 2> = d.<primary key column 2> AND
           i.ApprovedDate != d.ApprovedDate --Not sure what actual check you wanted to perform
END

Where deleted and inserted are pseudo-tables that contain the row(s) that the UPDATE statement has affected (corresponding to their state before and after the statement)

There's a function, called UPDATE which answers the question "was this column subject to an update during this UPDATE statement?" but it a) Only answers for the entire set of rows in inserted and deleted and b) doesn't let you distinguish updates that has no actual effects from those that did (eg if you do SET Column=2 where Column is already 2 , it still answers that Column was updated)


As an example of how pointless I think the UPDATE function is, consider the following:

create table T (ID int not null,Col1 int not null)
go
create trigger TT on T after update
as
    IF UPDATE(Col1)
    BEGIN
        RAISERROR('Hello',10,1) WITH NOWAIT
    END
go
update T set Col1 = Col1

Which prints Hello in the output - so, we have an UPDATE that affected 0 rows (because the table is new) and even if there were any rows, would not have changed any data.

use the inserted table, it will contain the new row(s). deleted will contain the old row(s).

You could do something like this...

INSERT INTO TargetTable(Col1, COl2, Col3)
SELECT t.Col1, t.COl2, t.Col3
FROM TableName t
WHERE EXISTS
         (
          SELECT 1
          FROM inserted i INNER JOIN deleted d 
          ON i.PK_Col = d.PK_Col
          WHERE i.Date_Col <> d.Date_Col
          AND d.PK_Col = t.PK_Col
          )

NOTE:- Posting this as a separate comment, as I don't have enough points to add comments.

You have two options for this, UPDATE() and COLUMNS_UPDATED() . Posting a sample script which uses UPDATE() function, External links are provided for UPDATE() and COLUMNS_UPDATED(). Please have a look on those links for a detailed description.

use TempDb 
Go 
create table test ( n1 int , n2 int , n3 int ) 
go 
create table testCopy ( n1 int , n2 int , n3 int ) 
go 
create trigger TestTrg
on  test
after update 
as 
if update(n3)  -- This is the key function
               -- http://technet.microsoft.com/en-us/library/ms186329.aspx -- COLUMNS_UPDATED() 
               -- http://technet.microsoft.com/en-us/library/ms187326.aspx -- UPDATE() 
   insert into testCopy (n3 )  select n3 from deleted
go 
insert into test values ( 1, 2, 3 ) ,( 2, 3, 4) 
go 

update test set n2 = n2 + 1  -- no values would be inserted into testcopy
update test set n1 = n1 + 1  -- no values would be inserted into testcopy

update test set n3 = n3 +1 where n3 = 3  -- one row will be inserted into test copt

This is simple, you can use the UPDATE function for checking field value update.

ALTER TRIGGER dbo.OrderApprovedSms 
   ON  dbo.[Order]
   AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    /*How can check approve date is updated*/
    IF (UPDATE (ApproveDate))
    BEGIN
       INSERT INTO office.SmsSendBuffer
               ( Number ,
                 Body 
               )
       SELECT 'xxxxxx','ORDER APPROVED!'
    END

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM