From: Josh Berkus <josh@agliodbs.com>
To: Michiel Lange <michiel@minas.demon.nl>
To: pgsql-sql@postgresql.org
Cc: Michiel Lange <michiel@minas.demon.nl>
Cc: pgsql-sql@postgresql.org
Subject: Re: trying to learn plpqsql... so please forgive..
Date: Wed, 20 Nov 2002 14:27:41 -0800
Message-ID: <200211201427.41550.josh@agliodbs.com> (raw)
In-Reply-To: <5.1.0.14.0.20021120225235.00b71db8@192.168.1.3>
References: <5.1.0.14.0.20021120010436.00b4c920@192.168.1.3>
<5.1.0.14.0.20021120225235.00b71db8@192.168.1.3>
Michiel,
> And this function:
> CREATE FUNCTION add_cust() RETURNS INT4 AS ' -- SERIAL data type is really
> an INT4 (and some more).
> BEGIN
> RETURN NEW.my_key;
> END;
> ' LANGUAGE 'plpgsql';
>
> CREATE TRIGGER add_cust BEFORE INSERT ON mytable
> FOR EACH ROW EXECUTE PROCEDURE add_cust();
>
> Ok, now I know it won't work... the idea was to use this with PHP in a
> webclient interface where the customer could give some information about
> him/herself and then would be registered with the customer number generated
> by the SERIAL type.
> Would it work if I did a CREATE TRIGGER add_cust AFTER INSERT... ? (mention
> the AFTER instead of BEFORE)
No, you can't return a value to the client from a Trigger. Not ever.
Triggers modify data, and they can log stuff, but they can't return values to
the calling interface.
Now, what you could do is replace the whole insert with a function, doing:
SELECT add_cust( name, address, phone, credit_card);
Which does the inserting and returns the new id to the client. This is a
solution I frequently use in my web apps.
-Josh Berkus
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: josh@agliodbs.com, michiel@minas.demon.nl
Subject: Re: trying to learn plpqsql... so please forgive..
In-Reply-To: <200211201427.41550.josh@agliodbs.com>
* 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