[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieving last created sequence index

Posted on 2002-05-29
6
Medium Priority
?
1,875 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 300 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 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