Thread: Return row after a Insert

Return row after a Insert

From
Carel Combrink
Date:
I have a Table that looks something like this:

CREATE TABLE My_Table(
    Entry        integer PRIMARY KEY DEFAULT nextval('Number'),
    TimeInsert    timestamp DEFAULT current_timestamp,
    Severity    ENUM_Severity NOT NULL,
    String    varchar(512)
);

where Number is a SEQUENCE and ENUM_Severity is an ENUM

There are 2 things I want to do.

After an INSERT statement I want to be able to know what was the
'Entry' number I just added. I was thinking on creating an AFTER
trigger returning the new ROW or just the 'Entry'-number but the
documentation on triggers state that the return is ignored on a AFTER
trigger. How can I get this information. (I want to use this in a C
function, using SPI I assume).

I also want to use the ENUM 'ENUM_Severity' in my C functions (using
v1 calling). I want to create a function looking like this:
CREATE FUNCTION add_to_my_table(ENUM_Severity, text) RETURNS integer
    AS 'myLibFile', 'add_to_my_table'
    LANGUAGE C;
and then in the function insert the passed arguments into the table.
How do I handle the ENUM in my function (c function, dynamic library).
What function should I use to get the correct value (PG_GETARG_XXX(0);)?

Then this comes together. I want to call the function to insert the
new row and then return the new number if the user want to look at the
new entry after the function call.

Please help, and if it helps this is my 2nd week using postgreSQL (any
DB for that matter)

--
Carel Combrink
s25291930@tuks.co.za

This message and attachments are subject to a disclaimer. Please refer
to www.it.up.ac.za/documentation/governance/disclaimer/ for full
details. / Hierdie boodskap en aanhangsels is aan 'n vrywaringsklousule
onderhewig. Volledige besonderhede is by
www.it.up.ac.za/documentation/governance/disclaimer/ beskikbaar.



Re: Return row after a Insert

From
"A. Kretschmer"
Date:
In response to Carel Combrink :
> I have a Table that looks something like this:
>
> CREATE TABLE My_Table(
>     Entry        integer PRIMARY KEY DEFAULT nextval('Number'),
>     TimeInsert    timestamp DEFAULT current_timestamp,
>     Severity    ENUM_Severity NOT NULL,
>     String    varchar(512)
> );
>
> where Number is a SEQUENCE and ENUM_Severity is an ENUM
>
> There are 2 things I want to do.
>
> After an INSERT statement I want to be able to know what was the
> 'Entry' number I just added. I was thinking on creating an AFTER

You can use RETURNING *
http://www.postgresql.org/docs/current/static/sql-insert.html

Or just for the SERIAL currval().


Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99