473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Getting last inserted SERIAL

Given this table:

CREATE TABLE test ( id SERIAL, example TEXT );

An implicit sequence is created as show in this message:

NOTICE: CREATE TABLE will create implicit sequence 'test_id_seq' for
SERIAL column 'test.id'

How do I retrieve the last 'id' that was inserted? I have a process
that does an insert and then needs the value of the id column of the row
it just inserted so that that row can be used later in processing.
First thought is "SELECT CURRVAL('test_id_seq');" but this assumes that
there is only one connection inserting into this table, bad assumption.
Next idea is the make 'id' an INT4 and set it explicitly with a value
that I select from an explicitly created sequence. This method seems a
bit inelegant. Any ideas?

TIA
Michael Garriss
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #1
2 7541
On Sun, 31 Aug 2003, mgarriss wrote:
First thought is "SELECT CURRVAL('test_id_seq');" but this assumes that
there is only one connection inserting into this table, bad assumption.


That is what you should use, and it works for concurrent sessions. It's
all described in the manual:

http://www.postgresql.org/docs/7.3/s...-sequence.html

--
/Dennis
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #2

Read the FAQ. Your currval assumption is wrong.

---------------------------------------------------------------------------

mgarriss wrote:
Given this table:

CREATE TABLE test ( id SERIAL, example TEXT );

An implicit sequence is created as show in this message:

NOTICE: CREATE TABLE will create implicit sequence 'test_id_seq' for
SERIAL column 'test.id'

How do I retrieve the last 'id' that was inserted? I have a process
that does an insert and then needs the value of the id column of the row
it just inserted so that that row can be used later in processing.
First thought is "SELECT CURRVAL('test_id_seq');" but this assumes that
there is only one connection inserting into this table, bad assumption.
Next idea is the make 'id' an INT4 and set it explicitly with a value
that I select from an explicitly created sequence. This method seems a
bit inelegant. Any ideas?

TIA
Michael Garriss
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
1
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Informix with ODBC. All the tables have a serial column, this is the ID for the record. I need to know how I can get the value of the...
4
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Oracle with ODBC. All the tables have an Identity column, this is the ID for the record. I need to know how I can get the value of...
6
by: Maurizio Faini | last post by:
I have a little problem. there is a way to get last id inserted into db or i have to make a new query? I explain better my question: in vbscript using sqlserver2000 i can use this code: ...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
10
by: MaRCeLO PeReiRA | last post by:
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
4
by: Normann | last post by:
I am creating a Stored Proc and I need to be able to select the last added row, now this should be made easier by the fact that I have a smalldatetime column in the table that is added every time a...
3
by: Rainy | last post by:
Hello! I'm having some trouble with pyserial package, I'm sending commands and reading responses from a custom pcb, and sometimes I get a proper response, at other times I get nothing, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.