Search Postgresql Archives

Re: comparing NEW and OLD (any good this way?)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello

http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks#Fast_compare_variables_NEW_and_OLD_in_trigger.27s_body

regards
Pavel Stehule

2009/7/23 Willy-Bas Loos <willybas@xxxxxxxxx>:
> Hi,
>
> My colleage Geard Troost and I found a handy way of comparing OLD and
> NEW in a trigger function.
> Normally this does not work (if anyone can tell me why, that'd be
> great), but once you cast them to text, it does.
>
> Is there anything to say against this, or can i go ahead and recommend
> this to everyone who wants to check if anything changed before doing
> what their update triggers do?
>
> Cheers,
>
> WBL
>
> Here's the code:
>
> drop table test;
> create table test (id integer primary key, value integer);
> insert into test values (1,1);
> insert into test values (2,1);
> insert into test values (3,1);
> insert into test values (4,1);
> insert into test values (5,1);
> insert into test values (6,1);
>
> create or replace function bla() returns trigger as $$
> begin
> IF (NEW::TEXT = OLD::TEXT) THEN raise notice 'changed';
> END IF;
> return NEW;
> end
> $$
> language plpgsql;
>
> CREATE TRIGGER test_bla BEFORE UPDATE
>   ON test FOR EACH ROW
>   EXECUTE PROCEDURE public.bla();
>
> update test set value =NULL where id= 1;
> update test set value =NULL where id= 1;
>
> --
> "Patriotism is the conviction that your country is superior to all
> others because you were born in it." -- George Bernard Shaw
>
> --
> Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux