| From: | Ian Barwick <barwick(at)gmx(dot)net> |
|---|---|
| To: | "kiyo taka" <hihajime(at)hotmail(dot)com>, pgsql-sql(at)postgresql(dot)org |
| Subject: | Re: plpgsql before insert update delete |
| Date: | 2001-12-08 03:01:00 |
| Message-ID: | 200112080359.EAA28586@post.webmailer.de |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-sql |
On Wednesday 05 December 2001 06:47, kiyo taka wrote:
> hi all.
>
> i'd like to noe how to create trigger which will insert tablez old data
> into an archive table before any(insert,update,delete) operation is
> executed.
>
> i've tried "if inserting ...." but it didnt work.
> is there any function that will do it?
Assuming an example table thus:
CREATE TABLE mytable(id INTEGER, whatever VARCHAR(16));
and an identical archive table:
CREATE TABLE mytable_archive(id INTEGER, whatever VARCHAR(16));
you can create the following function and trigger for that
table:
DROP FUNCTION mytable_archive_proc();
CREATE FUNCTION mytable_archive_proc()
RETURNS opaque
AS '
BEGIN
IF TG_OP = ''DELETE''
THEN INSERT INTO mytable_archive VALUES(old.id, old.whatever);
RETURN old;
ELSE INSERT INTO mytable_archive VALUES(new.id, new.whatever);
END IF;
RETURN new;
END;'
LANGUAGE 'plpgsql';
DROP TRIGGER mytable_archive_trigger ON mytable;
CREATE TRIGGER mytable_archive_trigger
BEFORE INSERT OR UPDATE OR DELETE
ON mytable
FOR EACH ROW
EXECUTE PROCEDURE mytable_archive_proc();
Which should write all changes in "mytable" to "mytable_archive".
> any help will be greatly appreciated.
>
> thanx.
Dou itashimashite
Ian Barwick
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Dominic Da Silva | 2001-12-09 20:29:24 | Poolman/mySQL transaction exception on connect |
| Previous Message | Ligia Pimentel | 2001-12-07 22:06:02 | How to rename a database? |