Re: Using oid as pkey - Mailing list pgsql-general

From D. Dante Lorenso
Subject Re: Using oid as pkey
Date
Msg-id 46CA2B20.6010804@lorenso.com
Whole thread Raw
In response to Re: Using oid as pkey  (Rainer Bauer <usenet@munnin.com>)
Responses Re: Using oid as pkey  (hubert depesz lubaczewski <depesz@depesz.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: "Joey K."
Date:
Subject: Re: Seeking datacenter PITR backup procedures [RESENDING]
Next
From: novice
Date:
Subject: Re: Join query help