Re: currval/sequence - Mailing list pgsql-novice

From Jason Hihn
Subject Re: currval/sequence
Date
Msg-id NGBBLHANMLKMHPDGJGAPOEGICMAA.jhihn@paytimepayroll.com
Whole thread Raw
In response to currval/sequence  ("cristi" <cristi@dmhi.ct.ro>)
Responses Re: currval/sequence
List 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@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
>


pgsql-novice by date:

Previous
From: Aled Morris
Date:
Subject: using procedural languages...
Next
From: Nabil Sayegh
Date:
Subject: Re: currval/sequence