Re: no results for nextval() query? - Mailing list pgsql-novice

From Richard Broersma Jr.
Subject Re: no results for nextval() query?
Date
Msg-id 4670D0A4.8040609@yahoo.com
Whole thread Raw
In response to Re: no results for nextval() query?  (Richard Broersma Jr <rabroersma@yahoo.com>)
List 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.


pgsql-novice by date:

Previous
From: "Richard Broersma Jr."
Date:
Subject: Re: inputs for pg_get_id() function?
Next
From: Brian Hurt
Date:
Subject: cannot cast type boolean to text?