Solved

ORACLE 10g: Function - insert unique identifier using sequence

Posted on 2015-01-27
7
569 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
  • 3
  • 3
7 Comments
 
LVL 73

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 73

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 73

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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 set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now