Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieving last created sequence index

Posted on 2002-05-29
6
Medium Priority
?
1,874 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

618 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