Re: currval/sequence

Lists: pgsql-novice
From: "cristi" <cristi(at)dmhi(dot)ct(dot)ro>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: currval/sequence
Date: 2003-08-15 09:22:50
Message-ID: 005101c3630e$cd28c900$7201a8c0@dmhi.ct.ro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

What is wrong here?

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


From: Jason Hihn <jhihn(at)paytimepayroll(dot)com>
To: cristi <cristi(at)dmhi(dot)ct(dot)ro>, pgsql-novice(at)postgresql(dot)org
Subject: Re: currval/sequence
Date: 2003-08-15 13:01:57
Message-ID: NGBBLHANMLKMHPDGJGAPOEGICMAA.jhihn@paytimepayroll.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

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(at)postgresql(dot)org
> [mailto:pgsql-novice-owner(at)postgresql(dot)org]On Behalf Of cristi
> Sent: Friday, August 15, 2003 5:23 AM
> To: pgsql-novice(at)postgresql(dot)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
>


From: Nabil Sayegh <postgresql(at)e-trolley(dot)de>
To: Jason Hihn <jhihn(at)paytimepayroll(dot)com>
Cc: cristi <cristi(at)dmhi(dot)ct(dot)ro>, pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: currval/sequence
Date: 2003-08-15 13:41:54
Message-ID: 1060954914.21417.1.camel@billy
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

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