Thread: How to notice column changes in trigger

How to notice column changes in trigger

From
Andreas Pflug
Date:
How can I detect whether a column was changed by an update command
inside a trigger?

create table test(a int, b int, c int, primary key(a))

b and c should be updated inside an update trigger if not modified by
the statement itself

1) update test set a=0 -> trigger does its work
2) update test set a=0, b=1, c=2 -> trigger does nothing
3) update test set a=0, b=b, c=c -> trigger does nothing, but content of
a and b dont change either although touched

What I'm looking for is something like
IF NOT COLUMN_TOUCHED(b) THEN ...
For MSSQL, this would be coded as  IF NOT UPDATE(b) ..

Any hints?

Andreas




Re: How to notice column changes in trigger

From
Oliver Elphick
Date:
On Thu, 2003-03-06 at 15:00, Andreas Pflug wrote:
> How can I detect whether a column was changed by an update command
> inside a trigger?
>
> create table test(a int, b int, c int, primary key(a))
>
> b and c should be updated inside an update trigger if not modified by
> the statement itself
>
> 1) update test set a=0 -> trigger does its work
> 2) update test set a=0, b=1, c=2 -> trigger does nothing
> 3) update test set a=0, b=b, c=c -> trigger does nothing, but content of
> a and b dont change either although touched
>
> What I'm looking for is something like
> IF NOT COLUMN_TOUCHED(b) THEN ...
> For MSSQL, this would be coded as  IF NOT UPDATE(b) ..

  IF NEW.b = OLD.b OR (NEW.b IS NULL AND OLD.b IS NULL) THEN
    -- b has not changed
    ...
  END IF;

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "The LORD is my light and my salvation; whom shall I
      fear? the LORD is the strength of my life; of whom
      shall I be afraid?"           Psalms 27:1


Re: How to notice column changes in trigger

From
Andreas Pflug
Date:
Oliver Elphick wrote:

>  IF NEW.b = OLD.b OR (NEW.b IS NULL AND OLD.b IS NULL) THEN
>    -- b has not changed
>    ...
>  END IF;
>
>
This doesn't cover case 3, since UPDATE ... SET b=b will lead to NEW.b=OLD.b



Re: How to notice column changes in trigger

From
Josh Berkus
Date:
Andreas,

> This doesn't cover case 3, since UPDATE ... SET b=b will lead to
> NEW.b=OLD.b

Why do you care about SET b = b?

And shouldn't this discussion be on the PGSQL-SQL list?

--
Josh Berkus
Aglio Database Solutions
San Francisco