From: Henshall, Stuart - Design & Print <SHenshall@westcountry-design-print.co.uk>
To: Michiel Lange <michiel@minas.demon.nl>
To: pgsql-sql@postgresql.org
Subject: Re: trying to learn plpqsql... so please forgive..
Date: Wed, 20 Nov 2002 09:48:13 -0000
Message-ID: <E382B5D8EDE1D6118DBE0008C759BCD6116AC4@WCPEXCHANGE> (raw)
Michiel Lange wrote:
> Maybe this should be sent to novice... I was not certain, but if it
> should, please tell me so.
>
> The matter at hand is this:
>
> When I do an 'INSERT INTO <table> VALUES <row1,row2,row3>'
> and on the table is a serial primary key named p_key.
> As I want this number to be auto-generated, but use it as a 'customer
> number', I want to create this function to return the value of this
> insert. I thought/hoped that this would work, but as there are some
> people dependant on this database, I dare not try out too much ;-)
> This would be the first time I used plpgsql, so I am not so certain
> about what I do.
>
> CREATE FUNCTION add_cust() RETURNS INT4 AS ' -- SERIAL data type is
> really an INT4 (and some more).
> BEGIN
> RETURN NEW.p_key;
> END;
> ' LANGUAGE 'plpgsql';
>
> CREATE TRIGGER add_cust BEFORE INSERT ON table
> FOR EACH ROW EXECUTE PROCEDURE add_cust();
>
>
> Someone willing to evaluate this for me, and telling me if it is safe
> to use as it is? or things I may do wrong?
>
> TIA,
> Michiel
>
>
Trigger functions can only return type OPAQUE which isn't seen by the client
program.
To get the value of the serial field for the last insert do:
SELECT currval('TableName_SerialFieldName_seq');
This will get the last value from the sequence used by this connection
(although it will error if no values have been requested).
hth,
- Stuart
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: SHenshall@westcountry-design-print.co.uk, michiel@minas.demon.nl
Subject: Re: trying to learn plpqsql... so please forgive..
In-Reply-To: <E382B5D8EDE1D6118DBE0008C759BCD6116AC4@WCPEXCHANGE>
References: <E382B5D8EDE1D6118DBE0008C759BCD6116AC4@WCPEXCHANGE>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox