Solved

Retrieving last created sequence index

Posted on 2002-05-29
6
1,848 Views
Last Modified: 2008-03-17
Let's say I have a table with 2 columns,

dogindex auto-generating sequence number
dogname varchar2(30)

If I insert a row into the table, how do I retrieve the last created index?  I am not looking for the following answer:

select max(dogindex) from dogtable

Rather, I'm looking for an Oracle equivalent of MS SQL Server's @@Identity system variable that will return the last created index.

Thanks.

0
Comment
Question by:BCao
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7042037
In Oracle I was used to query a new sequence number into a variable, and use that variable to create (insert) the new record. I could then use that variable to return to the application...

CHeers
0
 
LVL 2

Expert Comment

by:vbDoc
ID: 7042143
Ah, check out ROWNUM. The ROWNUM can also be used to update a table and to add a sequential number to each record. If a column named custiomer_id2 to  datatype NUMBER(7) was added to a table called s_customer, then the following update can be performed to uniquely number each record.

UPDATE s_customer
SET customer_id2 = ROWNUM;

HTH
John
0
 

Author Comment

by:BCao
ID: 7042156
Thanks guys for the replies, but unfortunately I cannot change the table design.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:joekendall
ID: 7042165
You could retrieve the currval from the sequence which should show the last value inserted into the table. Something like this would work:

SELECT SequenceName.currval FROM dual;

Thanks!

Joe
0
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 75 total points
ID: 7042204
Oracle tables do not directly support an "auto-generated" column the same way the MS SQL Server does.

Usually in Oracle "index" numbers like this for key values are generated by an Oracle sequence (I'm not sure that MS SQL Server has an object like an Oracle sequence.)  Insert statements typically get the next value with syntax like "sequence_name.nextval".  Let's assume you created a sequence named dog_seq and your table is named "dog_table".  Then in SQL*Plus your insert statement would look like this:

insert into dog_table
values (dog_seq.nextval, '&dog_name');

You could use a very similar statement in any other tool, just by substituting an appropriate variable for '&dog_name'.

If you have referenced that sequence in a session (with an insert statement that included "dog_seq.nextval") then you can query the database directly to see the last index value created.  Simply:
select dog_seq.lastval from dual;

If you haven't referenced that sequence though in your current database session, the only way to get the last value would be to select the highwater value from the sys.seq$ table.  But you need DBA privilege to do that, and you need to join it to sys.obj$ to use the sequence name.  Also, if the sequence was set up to cache values in the SGA, this query will show the last value cached, which wis usually higher than the last value actually used.

The bottom line is, in Oracle this particular question is not as easy to answer as it is in MS SQL Server.  I would usually get it by:
select max(dogindex) from dog_table;

This query will be fast if the column is indexed.  This is not the best way for an application to get the next value though (by selecting the max and adding 1) since multiple users could issue that same select at the same time.  Sequences handle that concurrency issue for you.
0
 

Author Comment

by:BCao
ID: 7042221
Thanks, your thourough answer was very insightful.

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now