Re: trying to learn plpqsql... so please forgive.. - Mailing list pgsql-sql

From Josh Berkus
Subject Re: trying to learn plpqsql... so please forgive..
Date
Msg-id 200211201427.41550.josh@agliodbs.com
Whole thread Raw
In response to Re: trying to learn plpqsql... so please forgive..  (Michiel Lange <michiel@minas.demon.nl>)
List pgsql-sql
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




pgsql-sql by date:

Previous
From: Michiel Lange
Date:
Subject: Re: trying to learn plpqsql... so please forgive..
Next
From: "Rajesh Kumar Mallah."
Date:
Subject: why the difference?