Thread: Question reagarding serial type.

Question reagarding serial type.

From
"Mirko Geffken"
Date:
Hi,

i have tried to find info elsewhere, but did not find anything to my satisfaction, so I was wondering if anyone on this
listcould point me into the right direction. 

When I create a table

e.g.:
create table xyz
(
  id    serial PRIMARY KEY,
  uvw   varchar2(100)
);

I would like to get the id returned when I insert into this table. In oracle it is possible to do this:
insert into xyz(uvw) values ('Test') returning id;

How can I do this with SQL in Postgres. Any ideas.

Thank you for any suggestions

  Mirko


Re: Question reagarding serial type.

From
Dustin Sallings
Date:
On Thu, 8 Jun 2000, Mirko Geffken wrote:

# i have tried to find info elsewhere, but did not find anything to my
# satisfaction, so I was wondering if anyone on this list could point me
# into the right direction.
#
# When I create a table
#
# e.g.:
# create table xyz
# (
#   id    serial PRIMARY KEY,
#   uvw   varchar2(100)
# );
#
# I would like to get the id returned when I insert into this table. In
# oracle it is possible to do this: insert into xyz(uvw) values ('Test')
# returning id;

    What if you had two serials?  I guess that's not terribly likely.

# How can I do this with SQL in Postgres. Any ideas.

insert into xyz(uvw) values ('Test');
INSERT 18968479 1
dustin=# select currval('xyz_id_seq');
 currval
---------
       1
(1 row)


--
dustin sallings                            The world is watching America,
http://2852210114/~dustin/                 and America is watching TV.


Re: Question reagarding serial type.

From
Ed Loehr
Date:
Mirko Geffken wrote:
>
> create table xyz
> (
>   id    serial PRIMARY KEY,
>   uvw   varchar2(100)
> );
>
> I would like to get the id returned when I insert into this table.

http://www.postgresql.org/docs/faq-english.html#4.16.2

Regards,
Ed Loehr