Re: Sequences

From: Chris Ruprecht <chrup999(at)yahoo(dot)com>
To: "Hunter, Ray" <rhunter(at)enterasys(dot)com>
Cc: "'Bruno Wolff III'" <bruno(at)wolff(dot)to>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Sequences
Date: 2002-02-11 17:25:48
Message-ID: p05101205b88da4231f87@[192.168.0.6]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<!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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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?&nbsp; 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.&nbsp; I am hoping it can all be done in sql
statements...</font><br>
<font size="-1">Example:</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size="-1"> var num =
select last_value from user_table_id_seq;</font><br>
</blockquote>
<blockquote type="cite"
cite>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size="-1"> insert
into users ( &quot;user_id&quot;, &quot;user_fname&quot;,
&quot;user_lname&quot;, &quot;user_email&quot; )</font><br>
<font size="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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">&nbsp; &quot;Hunter, Ray&quot;
&lt;rhunter(at)enterasys(dot)com&gt; wrote:</font><br>
<font size="-1">&gt; I have various sequences in my database set up
for ids.&nbsp; My question</font><br>
<font size="-1">&gt; is: How can I get the current value of the
sequence without creating a</font><br>
<font size="-1">&gt; session and using the currval
function?</font><br>
<font size="-1">&gt;</font><br>
<font size="-1">&gt; I would like to take the current value of the
sequence and use it for</font><br>
<font size="-1">&gt; a value in an insert statement.</font><br>
<font size="-1">&gt;</font><br>
<font size="-1">&gt;</font><br>
<font size="-1">&gt; Example:</font><br>
<font size="-1">&gt;</font><br>
<font size="-1">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert into
users ( &quot;user_id&quot;, &quot;user_fname&quot;,
&quot;user_lname&quot;,</font><br>
<font size="-1">&gt; &quot;user_email&quot; )</font><br>
<font size="-1">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; values (
'current sequence', 'Ray', 'Hunter',
'rhunter(at)enterasys(dot)com'</font><br>
<font size="-1">&gt; );</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 ( &quot;user_fname&quot;, &quot;user_lname&quot;,
&quot;user_email&quot; )</font><br>
</blockquote>
<blockquote type="cite" cite><font size="-1">&nbsp; 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

Attachment Content-Type Size
unknown_filename text/html 5.0 KB

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew G. Hammond 2002-02-11 17:54:08 Re: Oracle "Jobs" in PostgreSQL
Previous Message Bruno Wolff III 2002-02-11 15:51:37 Re: Sequences