Received: from ctron-dnm.ctron.com (ctron-dnm.enterasys.com [12.25.1.120]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g1BFIUE73979 for ; Mon, 11 Feb 2002 10:18:30 -0500 (EST) (envelope-from rhunter@enterasys.com) Received: (from uucp@localhost) by ctron-dnm.ctron.com (8.8.7/8.8.7) id KAA20486; Mon, 11 Feb 2002 10:28:01 -0500 (EST) Received: from unknown(134.141.77.96) by ctron-dnm.ctron.com via smap (4.1) id xmab20315; Mon, 11 Feb 02 10:24:55 -0500 Received: by cnc-exc1.enterasys.com with Internet Mail Service (5.5.2653.19) id <1VYXN2GY>; Mon, 11 Feb 2002 10:12:14 -0500 Message-ID: <59358A738F45D51186A30008C74CE250E81378@slc-exc1.ctron.com> From: "Hunter, Ray" To: "'Bruno Wolff III'" Cc: pgsql-sql@postgresql.org Subject: Re: Sequences Date: Mon, 11 Feb 2002 10:12:33 -0500 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C1B30E.87DD0ED0" X-Archive-Number: 200202/109 X-Sequence-Number: 6537 This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C1B30E.87DD0ED0 Content-Type: text/plain 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? This is just a hashed out example. I am hoping it can all be done in sql statements... Example: var num = select last_value from user_table_id_seq; insert into users ( "user_id", "user_fname", "user_lname", "user_email" ) values ( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' ); If I could do this is would be great...Is it possible? Thanks, Ray Hunter Firmware Engineer ENTERASYS NETWORKS -----Original Message----- From: Bruno Wolff III [mailto:bruno@wolff.to] Sent: Monday, February 11, 2002 7:37 AM To: Hunter, Ray Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] Sequences On Mon, Feb 11, 2002 at 08:43:23AM -0500, "Hunter, Ray" wrote: > I have various sequences in my database set up for ids. My question > is: How can I get the current value of the sequence without creating a > session and using the currval function? > > I would like to take the current value of the sequence and use it for > a value in an insert statement. > > > Example: > > insert into users ( "user_id", "user_fname", "user_lname", > "user_email" ) > values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com' > ); 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. 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" ) values ('Ray', 'Hunter', 'rhunter@enterasys.com' ); to add a row. ------_=_NextPart_001_01C1B30E.87DD0ED0 Content-Type: text/html Content-Transfer-Encoding: quoted-printable RE: [SQL] Sequences

The only problem with this solution is that I have alread= y incremented the sequence and need to use the current value?  Is ther= e a way to assign the value to a variable and then use it in an insert stat= ement?

This is just a hashed out example.  I am hoping it c= an all be done in sql statements...
Example:
        var num =3D s= elect last_value from user_table_id_seq;

        insert into us= ers ( "user_id", "user_fname", "user_lname", = "user_email" )
        values ( 'num= ', 'Ray', 'Hunter', 'rhunter@enterasys.com' );


If I could do this is would be great...Is it possible?


Thanks,

       =20

Ray Hunter
Firmware Engineer

ENTERASYS NETWORKS


-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Monday, February 11, 2002 7:37 AM
To: Hunter, Ray
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Sequences


On Mon, Feb 11, 2002 at 08:43:23AM -0500,
  "Hunter, Ray" <rhunter@enterasys.com= > wrote:
> I have various sequences in my database set up for = ids.  My question
> is: How can I get the current value of the sequence= without creating a
> session and using the currval function?
>
> I would like to take the current value of the seque= nce and use it for
> a value in an insert statement.
>
>
> Example:
>
>       insert into users ( = "user_id", "user_fname", "user_lname",
> "user_email" )
>       values ( 'current se= quence', 'Ray', 'Hunter', 'rhunter@enterasys.com'
> );

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.

If you make user_id a serial type, then its default value= will be nextval and you could use: insert into users ( "user_fname&qu= ot;, "user_lname", "user_email" )

  values ('Ray', 'Hunter', 'rhunter@enterasys.com' )= ;
to add a row.

------_=_NextPart_001_01C1B30E.87DD0ED0--