pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Ruprecht <chrup999@yahoo.com>
To: Hunter, Ray <rhunter@enterasys.com>
Cc: 'Bruno Wolff III' <bruno@wolff.to>
Cc: pgsql-sql@postgresql.org
Subject: Re: Sequences
Date: Mon, 11 Feb 2002 11:25:48 -0600
Message-ID: <p05101205b88da4231f87@[192.168.0.6]> (raw)
In-Reply-To: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com>
References: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com>

<!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@enterasys.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@wolff.to"><font
size="-1">mailto:bruno@wolff.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@postgresql.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@enterasys.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@enterasys.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@enterasys.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




view thread (19+ messages)  latest in thread

Message-ID: <p05101205b88da4231f87@[192.168.0.6]>
Permalink:  ../p05101205b88da4231f87@%5B192.168.0.6%5D/
Also on:    postgresql.org/message-id/p05101205b88da4231f87@[192.168.0.6]

 · 

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: chrup999@yahoo.com, rhunter@enterasys.com, bruno@wolff.to
  Subject: Re: Sequences
  In-Reply-To: <p05101205b88da4231f87@[192.168.0.6]>

* 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