Solved

ORACLE 10g: Function - insert unique identifier using sequence

Posted on 2015-01-27
7
578 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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 article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

778 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