473,387 Members | 1,700 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,387 software developers and data experts.

Insert and obtain the pk



Hello everybody,

Does anyone know an easy way to insert a record (without a value for the
primary key because it is a sequence) and at the same time obtain the value
of primary key that was assign to this new record.

I always try insert and then select with the same values that I used for the
insert, but I believe this is not a good way to do it.

Any suggestions???
Nov 12 '05 #1
3 1769
On Thu, Nov 06, 2003 at 12:26:48PM -0600, Eliu Montoya wrote:
Does anyone know an easy way to insert a record (without a value for the
primary key because it is a sequence) and at the same time obtain the value
of primary key that was assign to this new record.


Given that you are already using a sequence, you can obtain the last
value given to you by means of currval(). No, this doesn't have any
concurrency problems with multiple backends inserting at the same time;
that's exactly the problem it solves.

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"In Europe they call me Niklaus Wirth; in the US they call me Nickel's worth.
That's because in Europe they call me by name, and in the US by value!"

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
Eliu Montoya writes:
Does anyone know an easy way to insert a record (without a value for the
primary key because it is a sequence) and at the same time obtain the value
of primary key that was assign to this new record.


If you know the name of the sequence then you can use the function
currval() to get the last assigned value in your session.

--
Peter Eisentraut pe*****@gmx.net
---------------------------(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 12 '05 #3
You can call currval on the sequence that provided the value for the
primary key. It's guaranteed to be the last value retrieved from that
sequence *for your session*.

insert into table1 (f1, f2) values ('a', 'b');

select currval('table1_seq');

Use the name of your sequence. If the primary key is a SERIAL type,
then there will be a sequence in the background. I believe it's
tablename_seq or something like that.

regards, adam

On Nov 6, 2003, at 11:26 AM, Eliu Montoya wrote:
*

*

*

Hello everybody,

Does anyone know an easy way to *insert a record (without a value for
the primary key because it is a sequence) *and at the same time obtain
the value of primary key that was assign to this new record.

*

I always try insert and then select with the same values that I used
for the insert, but I believe this is not a good way to do it.

*

Any suggestions???


Nov 12 '05 #4

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

Similar topics

7
by: Beps | last post by:
hello, I have got a data list. In my Text widget each data is between the previous and the next; so they are whole in a only block. Now: |-------------------- |BushBerlusconiBlair|...
2
by: Gansoinat | last post by:
Hi, During an insert command, i'm using @@IDENTIFIER to obtain the key (auto increment colonne). 4 users insert record in the same time. result : user1 : Key=1 user2 : Key=2 user3 : Key=2...
3
by: Magnus Byne | last post by:
Hi, I have a problem using serializable transactions. In one transaction I select a single specific row from a table using a where clause (this causes it to acquire and hold a range lock). In...
9
by: adi | last post by:
Hi all, Hope there is a quick fix for this: I am inserting data from one table to another on the same DB. The insert is pretty simple as in: insert into datatable(field1, field2, field3)...
10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
7
by: Materialised | last post by:
Hi Folks, Just a quickie, I am inserting some records (individually) into a SQL Server Table. I am wondering how can I detect primary key violations on this data which I am inserting into the...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
4
bilibytes
by: bilibytes | last post by:
Hi, i am trying to INSERT into a table lets say 4 values: - value1 - value2 - value3 - value4 all these values are stored in variables($var1, $var2, $var3..), except one of them which is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.