In a Trigger, can I determine if a column was explicitly set to a values or not mentioned in update statement?

dba.stackexchange https://dba.stackexchange.com/questions/1604

  •  16-10-2019
  •  | 
  •  

Pergunta

Given a Table with a column col1 like this:

create table MyTest (
    col1 int NULL,
    col2 decimal(7, 2) NULL
);

insert into MyTest values ( 1, 1.1);

Is there a way for a trigger for update to decide which of the following update statements was executed ?

A:

update MyTest set col1 = 3;

B:

update MyTest set col1 = 3, col2 = 1.1;
Foi útil?

Solução

It makes a difference if this is going to be a set-based trigger, or if you are designing it to handle only one row at a time, but this should get you pointed in the right direction.

IF
UPDATE(col1)
AND UPDATE(col2)
AND EXISTS(SELECT * FROM inserted WHERE col1 = 3 AND col2 = 1.1)
BEGIN
    DO STUFF
END

IF 
UPDATE(col1) 
AND EXISTS(SELECT * FROM inserted WHERE col1 = 3)
BEGIN
    DO STUFF
END
Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top