"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                                [EMAIL PROTECTED]
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 



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to