getting currval from a sequence

Started by tonyabout 24 years ago10 messagesgeneral
Jump to latest
#1tony
tony@animaproductions.com

Hello,

I would like to be able to get currval from a sequence but am running
ito the "current session" limitation.

I need to get the id of the previous record either during a session or
on a cold start (next morning for example).

How does one get the last record from a series of records? I just need
the id field which is a sequence field

Cheers

Tony Grant

--
RedHat Linux on Sony Vaio C1XD/S
http://www.animaproductions.com/linux2.html
Macromedia UltraDev with PostgreSQL
http://www.animaproductions.com/ultra.html

#2Cornelia Boenigk
poppcorn@cornelia-boenigk.de
In reply to: tony (#1)
timespan_part()?

Hello

In a script I found a query

SELECT timespan_part( ... ) FROM table;

Is this a valid PostgreSQL-function? In the documentation I didn't
find any hint.
If yes, does PostgreSQL 7.1 support this function?

Thanks
Conni

#3Bruno Wolff III
bruno@wolff.to
In reply to: tony (#1)
Re: getting currval from a sequence

On Mon, Feb 11, 2002 at 11:42:31AM +0100,
tony <tony@animaproductions.com> wrote:

How does one get the last record from a series of records? I just need
the id field which is a sequence field

select max(id) from whatever;
(This assumes the sequence hasn't rolled over.)

#4Karel Zak
zakkr@zf.jcu.cz
In reply to: Cornelia Boenigk (#2)
Re: timespan_part()?

On Mon, Feb 11, 2002 at 02:35:21PM +0100, Cornelia Boenigk wrote:

Hello

In a script I found a query

SELECT timespan_part( ... ) FROM table;

Is this a valid PostgreSQL-function? In the documentation I didn't
find any hint.
If yes, does PostgreSQL 7.1 support this function?

Rather use date_part().

Karel

--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/

C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz

#5Cornelia Boenigk
poppcorn@cornelia-boenigk.de
In reply to: tony (#1)
Re: timespan_part()?

Hi Karel

Rather use date_part().

I know what to use instead and I know that I should use interval,
because timespan will be deprecated in future releases. Is PostgreSQL
such a future release?
My question is, weather a script with timespan_part() is working
properly in PostgreSQL 7.1.

Greetings
Conni

#6Thomas Lockhart
lockhart@fourpalms.org
In reply to: tony (#1)
Re: timespan_part()?

My question is, weather a script with timespan_part() is working
properly in PostgreSQL 7.1.

No. That function was deprecated for 7.0. And did not need to be called
explicitly in any version; date_part() was always the preferred choice.
afaicr...

- Thomas

#7Jan Poslusny
pajout@gingerall.cz
In reply to: tony (#1)
Re: getting currval from a sequence - BUG ?

Hi,
select currval('sequence_name');
with quotes should help you. But my pg 7.1.2 throws error
"sequence_name.currval is not yet defined in this session" (possibly bug
?). If I use sequence before calling this (for instance "insert into
mytable values(nextval('sequence_name'));" ), all is OK.

pajout

Bruno Wolff III wrote:

Show quoted text

On Mon, Feb 11, 2002 at 11:42:31AM +0100,
tony <tony@animaproductions.com> wrote:

How does one get the last record from a series of records? I just need
the id field which is a sequence field

select max(id) from whatever;
(This assumes the sequence hasn't rolled over.)

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

#8Doug McNaught
doug@wireboard.com
In reply to: tony (#1)
Re: getting currval from a sequence - BUG ?

Jan Poslusny <pajout@gingerall.cz> writes:

Hi,
select currval('sequence_name');
with quotes should help you. But my pg 7.1.2 throws error
"sequence_name.currval is not yet defined in this session" (possibly
bug ?). If I use sequence before calling this (for instance "insert
into mytable values(nextval('sequence_name'));" ), all is OK.

This is documented behavior. See the FAQ and the docs.

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
--T. J. Jackson, 1863

#9Darren Ferguson
darren@crystalballinc.com
In reply to: Jan Poslusny (#7)
Re: getting currval from a sequence - BUG ?

You are right with the currval but it is not a bug since you have not
initiated a nextval in the current session yet.

Check the list 3 - 4weeks back there was a nice thread about this and it
should hoepfully provide the answer

Darren

On Mon, 11 Feb 2002, Jan Poslusny wrote:

Show quoted text

Hi,
select currval('sequence_name');
with quotes should help you. But my pg 7.1.2 throws error
"sequence_name.currval is not yet defined in this session" (possibly bug
?). If I use sequence before calling this (for instance "insert into
mytable values(nextval('sequence_name'));" ), all is OK.

pajout

Bruno Wolff III wrote:

On Mon, Feb 11, 2002 at 11:42:31AM +0100,
tony <tony@animaproductions.com> wrote:

How does one get the last record from a series of records? I just need
the id field which is a sequence field

select max(id) from whatever;
(This assumes the sequence hasn't rolled over.)

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

#10Bruce Momjian
bruce@momjian.us
In reply to: Bruno Wolff III (#3)
Re: getting currval from a sequence

Bruno Wolff III wrote:

On Mon, Feb 11, 2002 at 11:42:31AM +0100,
tony <tony@animaproductions.com> wrote:

How does one get the last record from a series of records? I just need
the id field which is a sequence field

select max(id) from whatever;
(This assumes the sequence hasn't rolled over.)

You can do a select on the sequence table that lays behind the sequence.
usually ending in _seq. You can't use currval() because that is a
per-backend value that saves your previous nextval call. Make sure you
lock the sequence table during the retrieve if you want an accurate
number and don't unlock it until you are done with the value.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026