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