pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hunter, Ray <rhunter@enterasys.com>
To: 'Bruno Wolff III' <bruno@wolff.to>
Cc: pgsql-sql@postgresql.org
Subject: Re: Sequences
Date: Mon, 11 Feb 2002 10:12:33 -0500
Message-ID: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com> (raw)

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.

view thread (19+ messages)  latest in thread

Message-ID: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com>
Permalink:  ../59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com/
Also on:    postgresql.org/message-id/59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rhunter@enterasys.com, bruno@wolff.to
  Subject: Re: Sequences
  In-Reply-To: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox