Re: after update trigger ?

From: raghu ram <raghuchennuru(at)gmail(dot)com>
To: tmoore <tmoore(at)ttitech(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: after update trigger ?
Date: 2011-04-22 14:37:26
Message-ID: BANLkTi=xusL9fN+hzG7+AzDs9wkDFOqdJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Apr 22, 2011 at 7:23 PM, tmoore <tmoore(at)ttitech(dot)net> wrote:

> Can an after update trigger function do an insert into the the same table ?
>

Yes, we can do and code snippet as follows::

1. Fetch records from Existing Table

postgres=# SELECT * FROM NEW_EMP;
eno | ename | sal
-----+--------+-------
1 | xx | 9000
2 | ABC | 90000
(2 rows)

2. Create a function
CREATE OR REPLACE FUNCTION RAGHU() RETURNS TRIGGER
AS
$$
BEGIN
INSERT INTO NEW_EMP VALUES(1,'RAGHU',90000);
RETURN NEW;
END;
$$
LANGUAGE PLPGSQL;

3. Create a trigger
CREATE TRIGGER RAGHU_TRI AFTER UPDATE ON NEW_EMP FOR EACH ROW
EXECUTE PROCEDURE RAGHU();

4. Update the records in "NEW_EMP" table

postgres=# UPDATE NEW_EMP SET ENAME='RAJESH' WHERE ENO=2;
UPDATE 1

5. Fetch Records from "NEW_EMP" Table

postgres=# SELECT * FROM NEW_EMP;
eno | ename | sal
-----+--------+-------
1 | XX | 9000
2 | RAJESH | 90000
1 | RAGHU | 90000

Thanks & Regards
Raghu Ram
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

>
> Thanks
> Tom
>
>
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/after-update-trigger-tp4333008p4333008.html
> Sent from the PostgreSQL - novice mailing list archive at Nabble.com.
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2011-04-22 14:39:20 Re: after update trigger ?
Previous Message tmoore 2011-04-22 14:28:01 Re: after update trigger ?