Thread: Getting key value with an insert and using it in another.

Getting key value with an insert and using it in another.

From
"stefan"
Date:
Hi:

I need to be able to grab a key value generated by the server so that I can
use it with other inserts within the context of a transaction.

In a nut shell, I have an order table and an order details table. When an
order is saved, it has to go into both the order table and the order detail
tables where the order details records require the key value created in the
order table.

SQL server had a function to do this (@@identity) does PostGresSql have
anything?

Thanks,

Stefan



RE: Getting key value with an insert and using it in another.

From
"Dave Cramer"
Date:
Yes, they are called sequences in postgres.

Create table tablename (id serial, .... )

This creates a sequence named tablename_id_seq

You can then do a select on

Select next('tablename_id_seq')

Which will give you the next sequence and increment it

Dave

-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of stefan
Sent: July 16, 2001 1:06 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Getting key value with an insert and using it in
another.


Hi:

I need to be able to grab a key value generated by the server so that I
can use it with other inserts within the context of a transaction.

In a nut shell, I have an order table and an order details table. When
an order is saved, it has to go into both the order table and the order
detail tables where the order details records require the key value
created in the order table.

SQL server had a function to do this (@@identity) does PostGresSql have
anything?

Thanks,

Stefan



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)