Re: looping on NEW and OLD in a trigger

From: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
To: "Michael P(dot) Soulier" <michael_soulier(at)mitel(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: looping on NEW and OLD in a trigger
Date: 2010-08-28 21:18:29
Message-ID: AANLkTinbTY==MaNyMCh+-ZhGJAkv51ksMOVXv7OTsRrr@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hey Michael,

As of PostgreSQL 9.0 you can do it from PL/pgSQL by
using hstore module
(http://www.postgresql.org/docs/9.0/static/hstore.html)

I wrote an example for you:

CREATE TABLE person(id integer, fname text, lname text, birthday date);

CREATE TRIGGER person_test_trigger BEFORE INSERT
ON person FOR EACH ROW
EXECUTE PROCEDURE test_dynamic();

CREATE OR REPLACE FUNCTION test_dynamic()
RETURNS trigger
LANGUAGE plpgsql
AS $func$
DECLARE
_newRec hstore := hstore(NEW);
_field text;
BEGIN
FOR _field IN SELECT * FROM skeys(_newRec) LOOP
RAISE NOTICE '%', _field;
END LOOP;

RETURN NEW;
END;
$func$;

Regards,
Dmitriy

2010/8/26 Michael P. Soulier <michael_soulier(at)mitel(dot)com>

> Hi,
>
> I'm very new to writing postgres procedures, and I'm trying to loop over
> the fields in the NEW and OLD variables available in an after trigger,
> and I can't quite get the syntax correct.
>
> Could someone point me at an example?
>
> Thanks,
> Mike
> --
> Michael P. Soulier <michael_soulier(at)mitel(dot)com>, 613-592-2122 x2522
> "Any intelligent fool can make things bigger and more complex... It takes a
> touch of genius - and a lot of courage to move in the opposite direction."
> --Albert Einstein
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Fabrízio de Royes Mello 2010-08-28 22:23:23 Re: looping on NEW and OLD in a trigger
Previous Message Adrian Klaver 2010-08-28 20:25:07 Re: two different posgres t for Rails development?