Sequences
Sequences
I have various sequences in my database set up for ids. My question is: How can I get the current value of the sequence without creating a session and using the currval function?
I would like to take the current value of the sequence and use it for a value in an insert statement.
Example:
insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com' );Thanks,
Ray Hunter
Firmware EngineerENTERASYS NETWORKS
Phone: 801 887-9888
Fax: 801 972-5789
Email: rhunter@enterasys.com
www: www.enterasys.com
Re: Sequences
On Mon, Feb 11, 2002 at 08:43:23AM -0500, "Hunter, Ray" wrote:
> I have various sequences in my database set up for ids. My question is:
> How can I get the current value of the sequence without creating a session
> and using the currval function?
>
> I would like to take the current value of the sequence and use it for a
> value in an insert statement.
>
>
> Example:
>
> insert into users ( "user_id", "user_fname", "user_lname",
> "user_email" )
> values ( 'current sequence', 'Ray', 'Hunter',
> 'rhunter@enterasys.com' );
I think you want to use nextval in this context. currval is used when you
have already gotten a new sequence number and want to use it in several
inserts in the same transaction.
If you make user_id a serial type, then its default value will be nextval
and you could use:
insert into users ( "user_fname", "user_lname", "user_email" ) values ('Ray', 'Hunter', 'rhunter@enterasys.com' );
to add a row.
Re: Sequences
The only problem with this solution is that I have already incremented the sequence and need to use the current value? Is there a way to assign the value to a variable and then use it in an insert statement?
This is just a hashed out example. I am hoping it can all be done in sql statements...
Example:
var num = select last_value from user_table_id_seq;insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );If I could do this is would be great...Is it possible?
Thanks,
Ray Hunter
Firmware EngineerENTERASYS NETWORKS
-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Monday, February 11, 2002 7:37 AM
To: Hunter, Ray
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] SequencesOn Mon, Feb 11, 2002 at 08:43:23AM -0500,
"Hunter, Ray" <rhunter@enterasys.com> wrote:
> I have various sequences in my database set up for ids. My question
> is: How can I get the current value of the sequence without creating a
> session and using the currval function?
>
> I would like to take the current value of the sequence and use it for
> a value in an insert statement.
>
>
> Example:
>
> insert into users ( "user_id", "user_fname", "user_lname",
> "user_email" )
> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com'
> );I think you want to use nextval in this context. currval is used when you have already gotten a new sequence number and want to use it in several inserts in the same transaction.
If you make user_id a serial type, then its default value will be nextval and you could use: insert into users ( "user_fname", "user_lname", "user_email" )
values ('Ray', 'Hunter', 'rhunter@enterasys.com' );
to add a row.
Re: Sequences
Further to Brunos' suggestion,if you require to use the same id for insertion in other tables (slaves) you should
assign the value to a variable and use that value for inserting
in other referencing tables eg$id = $dbh -> selectrow_array("select nextval('seq_name')"); (# perl specific)
insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( $id , 'Ray', 'Hunter', 'rhunter@enterasys.com' );insert into user_details ( "user_id", ..... other details .... )
values ( $id , .... other details .... );
"Hunter, Ray" wrote:
I have various sequences in my database set up for ids. My question is: How can I get the current value of the sequence without creating a session and using the currval function?
I would like to take the current value of the sequence and use it for a value in an insert statement.
Example:
insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com' );Thanks,
Ray Hunter
Firmware EngineerENTERASYS NETWORKS
Phone: 801 887-9888
Fax: 801 972-5789
Email: rhunter@enterasys.com
www: www.enterasys.com
Re: Sequences
hi hunter,
nextval increments the sequence in an atomic operation and returns the current value
of the sequence. It is transaction safe in the sense,if two webserver process do two transactions simultaneously they would still get different
current vals.so last_val should not be the approach.
forgive me if i am not understanding the intricacies of your
problem.
"Hunter, Ray" wrote:
The only problem with this solution is that I have already incremented the sequence and need to use the current value? Is there a way to assign the value to a variable and then use it in an insert statement?
This is just a hashed out example. I am hoping it can all be done in sql statements...
Example:
var num = select last_value from user_table_id_seq;insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );If I could do this is would be great...Is it possible?
Thanks,
Ray Hunter
Firmware EngineerENTERASYS NETWORKS
-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Monday, February 11, 2002 7:37 AM
To: Hunter, Ray
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] SequencesOn Mon, Feb 11, 2002 at 08:43:23AM -0500,
"Hunter, Ray" <rhunter@enterasys.com> wrote:
> I have various sequences in my database set up for ids. My question
> is: How can I get the current value of the sequence without creating a
> session and using the currval function?
>
> I would like to take the current value of the sequence and use it for
> a value in an insert statement.
>
>
> Example:
>
> insert into users ( "user_id", "user_fname", "user_lname",
> "user_email" )
> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com'
> );I think you want to use nextval in this context. currval is used when you have already gotten a new sequence number and want to use it in several inserts in the same transaction.
If you make user_id a serial type, then its default value will be nextval and you could use: insert into users ( "user_fname", "user_lname", "user_email" )
values ('Ray', 'Hunter', 'rhunter@enterasys.com' );
to add a row.
Re: Sequences
On Mon, Feb 11, 2002 at 10:12:33AM -0500, "Hunter, Ray" wrote: > The only problem with this solution is that I have already incremented the > sequence and need to use the current value? Is there a way to assign the > value to a variable and then use it in an insert statement? You should be doing this in one transaction in which case currval should work. If you aren't doing it in one transaction, then your application will need to remember the current value.
Re: Sequences
On Mon, 2002-02-11 at 14:43, Hunter, Ray wrote: > I have various sequences in my database set up for ids. My question is: > How can I get the current value of the sequence without creating a session > and using the currval function? > > I would like to take the current value of the sequence and use it for a > value in an insert statement. > > > Example: > > insert into users ( "user_id", "user_fname", "user_lname", > "user_email" ) > values ( 'current sequence', 'Ray', 'Hunter', > 'rhunter@enterasys.com' ); > You probably wouldn't want to use the current but the next value of the sequence. Common solution is to set the default for the column user_id to something like nextval(users_user_id_seq"::text) and omit user_id in the insert statement such that the default value is taken. Markus Bertheau Cenes Data GmbH
Re: Sequences
The only problem with this solution is that I have already incremented the sequence and need to use the current value? Is there a way to assign the value to a variable and then use it in an insert statement?
This is just a hashed out example. I am hoping it can all be done in sql statements...
Example:
var num = select last_value from user_table_id_seq;
insert into users ( "user_id", "user_fname", "user_lname", "user_email" )
values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );
If I could do this is would be great...Is it possible?
Thanks,
Ray Hunter
Firmware Engineer
ENTERASYS NETWORKS
-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Monday, February 11, 2002 7:37 AM
To: Hunter, Ray
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Sequences
On Mon, Feb 11, 2002 at 08:43:23AM -0500,
"Hunter, Ray" <rhunter@enterasys.com> wrote:
> I have various sequences in my database set up for ids. My question
> is: How can I get the current value of the sequence without creating a
> session and using the currval function?
>
> I would like to take the current value of the sequence and use it for
> a value in an insert statement.
>
>
> Example:
>
> insert into users ( "user_id", "user_fname", "user_lname",
> "user_email" )
> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com'
> );
I think you want to use nextval in this context. currval is used when you have already gotten a new sequence number and want to use it in several inserts in the same transaction.
If you make user_id a serial type, then its default value will be nextval and you could use: insert into users ( "user_fname", "user_lname", "user_email" )
values ('Ray', 'Hunter', 'rhunter@enterasys.com' );
to add a row.
--
Re: Sequences
Andre Rothe writes:
> Where are stored the sequence information? How I can query the
> properties of a sequence like increment, max/min value, cache?
> I'm looking for a table like user_sequences in Oracle, where I
> can query all of my sequences.
cbbrowne=# create sequence foo;
CREATE SEQUENCE
cbbrowne=# select * from foo;sequence_name | last_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------foo | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f
(1 row)
Each sequence is effectively a relation.
--
output = ("cbbrowne" "@" "cbbrowne.com")
http://www3.sympatico.ca/cbbrowne/wp.html
Where do you want to Tell Microsoft To Go Today?
Re: Sequences
Andre Rothe <arothe@phosco.info> writes:
> Where are stored the sequence information? How I can query the
> properties of a sequence like increment, max/min value, cache?
> I'm looking for a table like user_sequences in Oracle, where I
> can query all of my sequences.
cbbrowne=# create sequence foo;
CREATE SEQUENCE
cbbrowne=# select * from foo;
sequence_name | last_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
foo | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f
(1 row)
Each sequence is effectively a relation.
--
output = ("cbbrowne" "@" "cbbrowne.com")
http://www3.sympatico.ca/cbbrowne/wp.html
Where do you want to Tell Microsoft To Go Today?
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
--
Mit freundlichen Grüßen
Dizdarevic Jasmin
Sonnenbergstr. 3
6714 Nüziders, AUT
jasmin.dizdarevic@gmail.com
+43 664 411 79 29
Re: Sequences
Easiest way is with pg_dump -s -t tablename dbname
On Sat, Jul 4, 2009 at 6:35 AM, Jasmin
Dizdarevic wrote:
> Nice Information. Does somebody know how to get the complete
> create-statement of an existing table/view?
>
> 2009/7/3 Chris Browne
>>
>> Andre Rothe writes:
>> > Where are stored the sequence information? How I can query the
>> > properties of a sequence like increment, max/min value, cache?
>> > I'm looking for a table like user_sequences in Oracle, where I
>> > can query all of my sequences.
>>
>> cbbrowne=# create sequence foo;
>> CREATE SEQUENCE
>> cbbrowne=# select * from foo;
>> sequence_name | last_value | increment_by | max_value |
>> min_value | cache_value | log_cnt | is_cycled | is_called
>>
>> ---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
>> foo | 1 | 1 | 9223372036854775807 |
>> 1 | 1 | 1 | f | f
>> (1 row)
>>
>> Each sequence is effectively a relation.
>> --
>> output = ("cbbrowne" "@" "cbbrowne.com")
>> http://www3.sympatico.ca/cbbrowne/wp.html
>> Where do you want to Tell Microsoft To Go Today?
>>
>> --
>> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql
>
>
>
> --
> Mit freundlichen Grüßen
>
> Dizdarevic Jasmin
> Sonnenbergstr. 3
> 6714 Nüziders, AUT
>
> jasmin.dizdarevic@gmail.com
> +43 664 411 79 29
>
>
--
When fascism comes to America, it will be intolerance sold as diversity.
Re: Sequences
Thanks, I have used SELECT sequence_name FROM information_schema.sequences WHERE sequence_catalog=? AND sequence_schema=? and then I can use select * from It seems to work :-) Andre Chris Browne wrote: > Andre Rothe writes: >> Where are stored the sequence information? How I can query the >> properties of a sequence like increment, max/min value, cache? >> I'm looking for a table like user_sequences in Oracle, where I >> can query all of my sequences. > > cbbrowne=# create sequence foo; > CREATE SEQUENCE > cbbrowne=# select * from foo; > sequence_name | last_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called > ---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+----------- > foo | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f > (1 row) > > Each sequence is effectively a relation.