Re: INSERT ... RETURNING as Oracle

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: "Sipos Andras" <s-andras-nospam4(at)freemail(dot)hu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: INSERT ... RETURNING as Oracle
Date: 2001-03-04 20:03:23
Message-ID: 200103042003.f24K3NG19970@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Sipos Andras" wrote: >create table basket (
> id serial NOT NULL PRIMARY KEY,
> timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
>);
>
>How can I make a one step insert into the table and get values of 'ID' ?
>I am trying to find a similar solution as in the oracle's INSERT ...
>RETURNING.
>
>If I use at first the INSERT, and after SELECT MAX(ID), the result will be
>uncertain.

The serial data type is actually an INT4 with a sequence, as you will have
seen when you created your table. Use currval after the insert to get the
latest value of the sequence in your current session.

junk=# create table basket (
junk(# id serial NOT NULL PRIMARY KEY,
junk(# timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
junk(# );
NOTICE: CREATE TABLE will create implicit sequence 'basket_id_seq' for SERIAL column 'basket.id'
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'basket_pkey' for table 'basket'
CREATE
junk=# insert into basket (timestamp) values (now());
INSERT 2091655 1
junk=# select currval('basket_id_seq');
currval
---------
1
(1 row)

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Give, and it will be given to you. A good measure,
pressed down, taken together and running over,
will be poured into your lap. For with the same
measure that you use, it will be measured to
you." Luke 6:38

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2001-03-04 20:04:02 Re: INSERT ... RETURNING as Oracle
Previous Message Sipos Andras 2001-03-04 19:17:59 INSERT ... RETURNING as Oracle