Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1CB16475FBD for ; Wed, 20 Nov 2002 04:50:52 -0500 (EST) Received: from wcpexchange.westcountrypublications.co.uk (unknown [195.171.163.155]) by postgresql.org (Postfix) with ESMTP id 768BF475C9E for ; Wed, 20 Nov 2002 04:50:50 -0500 (EST) Received: by WCPEXCHANGE with Internet Mail Service (5.5.2653.19) id ; Wed, 20 Nov 2002 09:48:14 -0000 Message-ID: From: "Henshall, Stuart - Design & Print" To: 'Michiel Lange' , pgsql-sql@postgresql.org Subject: Re: trying to learn plpqsql... so please forgive.. Date: Wed, 20 Nov 2002 09:48:13 -0000 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C29079.F1187670" X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/301 X-Sequence-Number: 10443 This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C29079.F1187670 Content-Type: text/plain; charset="iso-8859-1" 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 VALUES ' > 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 ------_=_NextPart_001_01C29079.F1187670 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: [SQL] trying to learn plpqsql... so please forgive..

Michiel Lange wrote:
> Maybe this should be sent to novice... I was not ce= rtain, 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_ke= y.
> As I want this number to be auto-generated, but use= it as a 'customer
> number', I want to create this function to return t= he value of this
> insert. I thought/hoped that this would work, but a= s there are some
> people dependant on this database, I dare not try o= ut too much ;-)
> This would be the first time I used plpgsql, so I a= m not so certain
> about what I do.
>
> CREATE FUNCTION add_cust() RETURNS INT4 AS ' -- SER= IAL data type is
> really an INT4 (and some more).
>      BEGIN
>        &nbs= p; 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 tellin= g 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 t= his connection (although it will error if no values have been requested).

hth,
- Stuart

------_=_NextPart_001_01C29079.F1187670--