Re: [SQL] Getting primary key from insert statement

Поиск
Список
Период
Сортировка
От D'Arcy" "J.M." Cain
Тема Re: [SQL] Getting primary key from insert statement
Дата
Msg-id m10pJu5-0000bIC@druid.net
обсуждение исходный текст
Ответ на Getting primary key from insert statement  ("Pham, Thinh" <tpham@mail.priority.net>)
Список pgsql-sql
Thus spake Pham, Thinh
> Hi, is there a way you can get back the primary key (serialized) from an
> insert you just do on a table. For example i have a table called "order" and
> i want to get the pid of that record so i can use it in the "orderdetail"
> table. It would be nice if that same insert statement could also return the
> pid.
> 
> Table structure:
> create table order (pid serial, orddate date);

You realize that order is a keyword, right?

> Insert statement:
> insert into order (date) values ('6/2/1999')

Assuming you change order to something else, the insert returns the OID
of the record just added.  In psql this appears as something like this.

INSERT 36448 1

So, just read back the record.

SELECT pid FROM table WHERE OID = 36448;

The various interfaces give you methods to get this value back.  Read the
docs for whichever interface you are using.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.


В списке pgsql-sql по дате отправления:

Предыдущее
От: "Mark Wright"
Дата:
Сообщение: Are PL/pgSQL calls atomic?
Следующее
От: Vadim Mikheev
Дата:
Сообщение: Re: [SQL] Are PL/pgSQL calls atomic?