Re: Sequences
От | mallah@trade-india.com |
---|---|
Тема | Re: Sequences |
Дата | |
Msg-id | 3C67E591.F2641FEA@trade-india.com обсуждение исходный текст |
Ответ на | Re: Sequences ("Hunter, Ray" <rhunter@enterasys.com>) |
Список | pgsql-sql |
hi hunter, <br /> <p>nextval increments the sequence in an atomic operation and returns the current value <br />of thesequence. It is transaction safe in the sense, <p>if two webserver process do two transactions simultaneously they wouldstill get different <br />current vals. <p>so last_val should not be the approach. <p>forgive me if i am not understanding the intricacies of your <br />problem. <br /> <br /> <p>"Hunter, Ray" wrote: <blockquote type="CITE"> <p><fontsize="-1">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="-1">This is just a hashed out example. I am hoping it can all be done in sql statements...</font><br/><font size="-1">Example:</font><br /> <font size="-1">var num = select last_value from user_table_id_seq;</font><p> <font size="-1">insert into users ( "user_id", "user_fname", "user_lname", "user_email")</font><br /><font size="-1"> values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );</font><p><fontsize="-1">If I could do this is would be great...Is it possible?</font><p><font size="-1">Thanks,</font><br/> <br /> <p><font size="-1">Ray Hunter</font><br /><font size="-1">Firmware Engineer</font><p><fontsize="-1">ENTERASYS NETWORKS</font><p><font size="-1">-----Original Message-----</font><br /><fontsize="-1">From: Bruno Wolff III [<a href="mailto:bruno@wolff.to">mailto:bruno@wolff.to</a>]</font><br /><font size="-1">Sent:Monday, February 11, 2002 7:37 AM</font><br /><font size="-1">To: Hunter, Ray</font><br /><font size="-1">Cc:pgsql-sql@postgresql.org</font><br /><font size="-1">Subject: Re: [SQL] Sequences</font><p><font size="-1">OnMon, Feb 11, 2002 at 08:43:23AM -0500,</font><br /><font size="-1"> "Hunter, Ray" <rhunter@enterasys.com>wrote:</font><br /><font size="-1">> I have various sequences in my database set up for ids. My question</font><br /><font size="-1">> is: How can I get the current value of the sequence without creating a</font><br/><font size="-1">> session and using the currval function?</font><br /><font size="-1">></font><br /><fontsize="-1">> I would like to take the current value of the sequence and use it for</font><br /><font size="-1">>a value in an insert statement.</font><br /><font size="-1">></font><br /><font size="-1">></font><br/><font size="-1">> Example:</font><br /><font size="-1">></font><br /><font size="-1">> insert into users ( "user_id", "user_fname", "user_lname",</font><br /><font size="-1">> "user_email")</font><br /><font size="-1">> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com'</font><br/><font size="-1">> );</font><p><font size="-1">I think you want to use nextval in thiscontext. currval is used when you have already gotten a new sequence number and want to use it in several inserts inthe same transaction.</font><p><font size="-1">If you make user_id a serial type, then its default value will be nextvaland you could use: insert into users ( "user_fname", "user_lname", "user_email" )</font><p><font size="-1"> values('Ray', 'Hunter', 'rhunter@enterasys.com' );</font><br /><font size="-1">to add a row.</font></blockquote>
В списке pgsql-sql по дате отправления: