Thread: currval/sequence

currval/sequence

From
"cristi"
Date:
What is wrong here?

insert into table_name (field_name) values (select
setval('sequence_name')-1) as currval);



Re: currval/sequence

From
Jason Hihn
Date:
What is it that you want to do?

You don't have to manually insert the values of the sequence.
create table test1(i char(1), s serial);
insert into test 1 values ('a');
select currval('test1_seq');  <- should return 1
insert into test 1 values ('b');
select currval('test1_seq');  <- should return 2

and the tuples should be:
a | s
-----
a | 1
b | 2

In my code, I wrap it in a transaction for peace of mind. Tom (or anyone),
is that necessary? If nothing else is happening on that connection between
the insert and the select?

Hope that helps.

> -----Original Message-----
> From: pgsql-novice-owner@postgresql.org
> [mailto:pgsql-novice-owner@postgresql.org]On Behalf Of cristi
> Sent: Friday, August 15, 2003 5:23 AM
> To: pgsql-novice@postgresql.org
> Subject: [NOVICE] currval/sequence
>
>
> What is wrong here?
>
> insert into table_name (field_name) values (select
> setval('sequence_name')-1) as currval);
>
>
>
> ---------------------------(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
>


Re: currval/sequence

From
Nabil Sayegh
Date:
Am Fre, 2003-08-15 um 15.01 schrieb Jason Hihn:

> In my code, I wrap it in a transaction for peace of mind. Tom (or anyone),
> is that necessary? If nothing else is happening on that connection between
> the insert and the select?

This is not necessary.
Everything you do with sequences will be unique to your connection.
--
 e-Trolley Sayegh & John, Nabil Sayegh
 Tel.: 0700 etrolley /// 0700 38765539
 Fax.: +49 69 8299381-8
 PGP : http://www.e-trolley.de