From: | Volkan YAZICI <yazicivo(at)ttnet(dot)net(dot)tr> |
---|---|
To: | Todd Kennedy <todd(dot)kennedy(at)gmail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Simple plpgsql question |
Date: | 2006-04-14 09:13:44 |
Message-ID: | 20060414091344.GA209@alamut |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Apr 13 11:38, Todd Kennedy wrote:
> What I'd also like to do is have it create a new row in a different
> table using the automatically assigned id as a reference, but I'm
> unsure of how to obtain the id of the newly created row in the first
> table.
If I understand you right, you're refering to a SERIAL column with id.
If so, you can use currval() function over related SEQUENCE. Because of
INSERT/DELETE and trigger will be executed in the same session, they'll
be able to see current value of related sequence. Below is an example
about this:
BEGIN;
CREATE SEQUENCE trig_t_seq START 1;
CREATE TABLE trig_t (
id bigint NOT NULL DEFAULT nextval('trig_t_seq'),
inf int
);
CREATE FUNCTION trig_t_row_count() RETURNS trigger AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
RAISE NOTICE 'Current SEQUENCE value: %', currval('trig_t_seq');
END IF;
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER trig_t_row_count_trig
AFTER INSERT
ON trig_t
FOR EACH ROW
EXECUTE PROCEDURE trig_t_row_count();
INSERT INTO trig_t (inf) VALUES (10);
INSERT INTO trig_t (inf) VALUES (20);
INSERT INTO trig_t (inf) VALUES (30);
ROLLBACK;
Regards.
From | Date | Subject | |
---|---|---|---|
Next Message | Achilleus Mantzios | 2006-04-14 10:26:02 | Kernel 2.4->2.6 upgrade results in PANIC: could not locate a valid checkpoint record |
Previous Message | Terry Lee Tucker | 2006-04-14 09:03:10 | Re: Simple plpgsql question |