Solved

Retrieving last created sequence index

Posted on 2002-05-29
6
1,866 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 35

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

718 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