Re: Using oid as pkey

Поиск
Список
Период
Сортировка
От D. Dante Lorenso
Тема Re: Using oid as pkey
Дата
Msg-id 46CA2B20.6010804@lorenso.com
обсуждение исходный текст
Ответ на Re: Using oid as pkey  (Rainer Bauer <usenet@munnin.com>)
Ответы Re: Using oid as pkey
Список 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 по дате отправления:

Предыдущее
От: "Joey K."
Дата:
Сообщение: Re: Seeking datacenter PITR backup procedures [RESENDING]
Следующее
От: novice
Дата:
Сообщение: Re: Join query help