Обсуждение: after update trigger ?

Поиск
Список
Период
Сортировка

after update trigger ?

От
tmoore
Дата:
Can an after update trigger function do an insert into the the same table ?

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.

Re: after update trigger ?

От
tmoore
Дата:
Sorry, one of those can't see the forest from the trees things ....
Tom

--
View this message in context: http://postgresql.1045698.n5.nabble.com/after-update-trigger-tp4333008p4333066.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.

Re: after update trigger ?

От
raghu ram
Дата:


On Fri, Apr 22, 2011 at 7:23 PM, tmoore <tmoore@ttitech.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@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice

Re: after update trigger ?

От
Tom Lane
Дата:
tmoore <tmoore@ttitech.net> writes:
> Can an after update trigger function do an insert into the the same table ?

Sure.  You have to avoid recursively triggering the same trigger, but an
insert wouldn't trigger the update trigger.

            regards, tom lane