<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { padding-top: 0 ; padding-bottom: 0 }
--></style><title>Re: [SQL] Sequences</title></head><body>
<div>Ray,</div>
<div><br></div>
<div>You can certainly store the value in a variable, how to do that
depends on the language you're using. If you're running PL/pgSQL, then
you would have to define the variable somewhere on top and then
use</div>
<div><x-tab> </x-tab></div>
<div>var_num := ...</div>
<div><br></div>
<div>Best regards,</div>
<div>Chris</div>
<div><br></div>
<div>At 10:12 -0500 02/11/2002, Hunter, Ray wrote:</div>
<blockquote type="cite" cite><font size="-1">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?</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-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><br>
</blockquote>
<blockquote type="cite"
cite> <font size="-1"> insert
into users ( "user_id", "user_fname",
"user_lname", "user_email" )</font><br>
<font size="-1"> values (
'num', 'Ray', 'Hunter', 'rhunter(at)enterasys(dot)com' );</font><br>
</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite><font size="-1">If I could do this is
would be great...Is it possible?</font><br>
</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite><font size="-1">Thanks,</font><br>
</blockquote>
<blockquote type="cite" cite> <br>
</blockquote>
<blockquote type="cite" cite><font size="-1">Ray Hunter</font><br>
<font size="-1">Firmware Engineer</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-1">ENTERASYS
NETWORKS</font><br>
</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite><font size="-1">-----Original
Message-----</font><br>
<font size="-1">From: Bruno Wolff III [</font><a
href="mailto:bruno(at)wolff(dot)to"><font
size="-1">mailto:bruno(at)wolff(dot)to</font></a><font size="-1">]</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(at)postgresql(dot)org</font><br>
<font size="-1">Subject: Re: [SQL] Sequences</font><br>
</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite><font size="-1">On Mon, Feb 11, 2002 at
08:43:23AM -0500,</font><br>
<font size="-1"> "Hunter, Ray"
<rhunter(at)enterasys(dot)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>
<font size="-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(at)enterasys(dot)com'</font><br>
<font size="-1">> );</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-1">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.</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-1">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" )</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-1"> values ('Ray',
'Hunter', 'rhunter(at)enterasys(dot)com' );</font><br>
<font size="-1">to add a row.</font></blockquote>
<div><br></div>
<div><br></div>
<x-sigsep><pre>--
</pre></x-sigsep>
<div align="center"><font color="#0000FF">Chris Ruprecht</font></div>
<div align="center"><font color="#0000FF">Network grunt and bit pusher
extraordinaíre</font></div>
</body>
</html>
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com