Solved

ORACLE 10g: Function - insert unique identifier using sequence

Posted on 2015-01-27
7
589 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 250 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 250 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

696 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