Re: INSERT ... RETURNING as Oracle - Mailing list pgsql-general

From Oliver Elphick
Subject Re: INSERT ... RETURNING as Oracle
Date
Msg-id 200103042003.f24K3NG19970@linda.lfix.co.uk
Whole thread Raw
In response to INSERT ... RETURNING as Oracle  ("Sipos Andras" <s-andras-nospam4@freemail.hu>)
List 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.Elphick@lfix.co.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



pgsql-general by date:

Previous
From: Louis-David Mitterrand
Date:
Subject: Re: avoiding endless loop in an UPDATE trigger
Next
From: Louis-David Mitterrand
Date:
Subject: Re: Trigger sending an eMail