Re: no results for nextval() query?

From: "Richard Broersma Jr(dot)" <rabroersma(at)yahoo(dot)com>
To: Steve Lefevre <lefevre(dot)10(at)osu(dot)edu>, pgsql-novice(at)postgresql(dot)org
Subject: Re: no results for nextval() query?
Date: 2007-06-14 05:22:44
Message-ID: 4670D0A4.8040609@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Steve Lefevre wrote:
> Richard Broersma Jr wrote:
>> nextval is a stand-alone function and does not belong to a table.
>>
>> Also nextval() does not operate on a column. i.e. nextval(
>> column_name ) is incorrect.
>>
>> nextval() operates on a PostgreSQL entity called a sequence, which
>> "acts" like a public variable
>> that is used to hold an ever increasing number.
>>
> So I can do "SELECT nextval('sequence_name')" or "SELECT
> currval('sequence_name')" ?
>
> I thought I had tried that at work, and got some kind of error. I'll
> look at it tomorow and report back.
>
I works for me:

mydb=# \ds
List of relations
Schema | Name | Type | Owner
--------+---------------------+----------+----------
public | apple_apple_nbr_seq | sequence | postgres
(1 row)

mydb=# select * from nextval('apple_apple_nbr_seq');
nextval
---------
4
(1 row)

mydb=# select nextval('apple_apple_nbr_seq');
nextval
---------
5
(1 row)

mydb=# INSERT INTO Apple( variety ) VALUES( 'Washington Apple' )
RETURNING apple_nbr ;
apple_nbr
-----------
6
(1 row)

INSERT 0 1
mydb=#
mydb=# insert into apple_orders( order_nbr, apple_nbr, qty ) values (
default, currval('apple_apple_nbr_seq'), 200);
INSERT 0 1
mydb=# select * from apple_orders;
order_nbr | apple_nbr | qty
-----------+-----------+-----
1 | 6 | 200
(1 row)

mydb=#

Regards,
Richard Broersma Jr.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Brian Hurt 2007-06-14 18:30:44 cannot cast type boolean to text?
Previous Message Richard Broersma Jr. 2007-06-14 05:06:17 Re: inputs for pg_get_id() function?