Re: Using oid as pkey

From: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
To: Rainer Bauer <usenet(at)munnin(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Using oid as pkey
Date: 2007-08-21 00:00:32
Message-ID: 46CA2B20.6010804@lorenso.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message novice 2007-08-21 00:52:46 Re: Join query help
Previous Message Joey K. 2007-08-20 23:57:01 Re: Seeking datacenter PITR backup procedures [RESENDING]