?
Solved

ORACLE 10g: Function - insert unique identifier using sequence

Posted on 2015-01-27
7
Medium Priority
?
610 Views
Last Modified: 2015-01-28
Experts,

I have a function that truncates a table and inserts new data.  The data that is being inserted doesn't have a unique ID nor does it have a field that is unique in records.  Therefore, I want to create a unique id for each row, which has to be numbers only.  Any ideas on how to accomplish this?

Example:
Colum1 (ID)
1
2
3
4
5
etc

Thanks!
0
Comment
Question by:Maliki Hassani
[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
  • 3
  • 3
7 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 40573928
if your inserted data is from a select statement, you can simply use ROWNUM

for example...

select rownum, t.* from all_tables t;


to create a sequence object is easy though

create sequence my_sequence;

insert into yourtable( id) values (my_sequence.nextval);'

or

select my_sequence.nextval,t.* from all_tables t;
0
 
LVL 23

Assisted Solution

by:David
David earned 1000 total points
ID: 40573930
Create a sequence, initialize it, and use the built-in function to increment it.  INSERT into table VALUES (somesequence.nextval)  is the simplest, and doesn't require any trigger.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40573943
also, if this is 12c,  you can associate a sequence with a column to create an "identity column" like you see in other databases.
0
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 

Author Comment

by:Maliki Hassani
ID: 40573947
Great I will give this a try!
0
 

Author Comment

by:Maliki Hassani
ID: 40573959
I used rownum and that that worked.  Do I need to have a sequence added and applied or just use rownum?
0
 

Author Comment

by:Maliki Hassani
ID: 40574293
Let me rephrase that..  as for speed is it better to use sequence or row number?  Thanks
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40575069
the difference will be very minor, but rownum should probably be minorly more efficient since it never has to update the data dictionary but a sequence with a large cache value will minimize that.

Best way to verify is to simply run both on your system and test results.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Suggested Courses

752 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