Rainer Bauer wrote:
"D. Dante Lorenso" wrote:

Using a brain-dead sample table that looks like this:

        CREATE table some_table (
                col0 SERIAL,
                col1 VARCHAR,
                col2 VARCHAR
        );

I want to do something like this:

        INSERT INTO some_table (col1, col2)
        VALUES ('val1', 'val2');

I want the value of col0 returned to the application and I don't want to know the name of the sequence involved in the SERIAL column. I just want the value inserted into the column by using just it's column name.

Using 8.2 or above:
INSERT INTO some_table (col1, col2) VALUES ('val1', 'val2') RETURNING col0;

Oh ... VERY NICE ;-) This is even BETTER than just returning the value of the SERIAL column since you can return any column even if there are more than one SERIAL columns in a table! No need for OID, no need for LASTVAL() ...

I see this from the documentation of 8.2:

---------- 8< -------------------- 8< ----------
The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted. This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table's columns is allowed. The syntax of the RETURNING list is identical to that of the output list of SELECT.
---------- 8< -------------------- 8< ----------

Exactly what I was looking for. Looks like I need to make moves to get from 8.1 onto 8.2 ;-)

Thanks, Rainer!

-- Dante

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to