Re: Sequences - Mailing list pgsql-sql
From | Hunter, Ray |
---|---|
Subject | Re: Sequences |
Date | |
Msg-id | 59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com Whole thread Raw |
In response to | Sequences ("Hunter, Ray" <rhunter@enterasys.com>) |
Responses |
Re: Sequences
Re: Sequences |
List | pgsql-sql |
<p><font size="2">The only problem with this solution is that I have already incremented the sequence and need to use thecurrent value? Is there a way to assign the value to a variable and then use it in an insert statement?</font><p><fontsize="2">This is just a hashed out example. I am hoping it can all be done in sql statements...</font><br/><font size="2">Example:</font><br /> <font size="2">var num = select last_value from user_table_id_seq;</font><p> <font size="2">insert into users ( "user_id", "user_fname", "user_lname", "user_email")</font><br /><font size="2"> values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );</font><br /><p><fontsize="2">If I could do this is would be great...Is it possible?</font><br /><p><font size="2">Thanks,</font><p> <p><font size="2">Ray Hunter</font><br /><font size="2">Firmware Engineer</font><p><fontsize="2">ENTERASYS NETWORKS</font><br /><p><font size="2">-----Original Message-----</font><br /><fontsize="2">From: Bruno Wolff III [<a href="mailto:bruno@wolff.to">mailto:bruno@wolff.to</a>] </font><br /><font size="2">Sent:Monday, February 11, 2002 7:37 AM</font><br /><font size="2">To: Hunter, Ray</font><br /><font size="2">Cc:pgsql-sql@postgresql.org</font><br /><font size="2">Subject: Re: [SQL] Sequences</font><br /><p><font size="2">OnMon, Feb 11, 2002 at 08:43:23AM -0500,</font><br /><font size="2"> "Hunter, Ray" <rhunter@enterasys.com>wrote:</font><br /><font size="2">> I have various sequences in my database set up for ids. My question </font><br /><font size="2">> is: How can I get the current value of the sequence without creating a</font><br /><font size="2">> session and using the currval function?</font><br /><font size="2">> </font><br /><fontsize="2">> I would like to take the current value of the sequence and use it for </font><br /><font size="2">>a value in an insert statement.</font><br /><font size="2">> </font><br /><font size="2">> </font><br/><font size="2">> Example:</font><br /><font size="2">> </font><br /><font size="2">> insert intousers ( "user_id", "user_fname", "user_lname", </font><br /><font size="2">> "user_email" )</font><br /><font size="2">> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com' </font><br /><font size="2">>);</font><p><font size="2">I think you want to use nextval in this context. currval is used when you have alreadygotten a new sequence number and want to use it in several inserts in the same transaction.</font><p><font size="2">Ifyou 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" )</font><p><font size="2"> values ('Ray', 'Hunter', 'rhunter@enterasys.com' );</font><br/><font size="2">to add a row.</font>