Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add new column to existing table and insert incremental sequence number

Posted on 2009-07-14
5
Medium Priority
?
1,077 Views
Last Modified: 2013-12-07
Hi,

We need to add a new column to a table and then populate this column with a sequence number based on some ordering of the current data.

i.e. we have a date in the table which we want to order by and then based on this order assign the correct sequence number hence if you order by date and order by sequence number you get the same order.

I was wondering if anybody knew the fastest way of doing this?

Currently the only option we have found is to take a copy of the table and order the data and then using a for loop insert into the column using a sequence . This seems like a slow method to me considering you can only use a for loop and not bulk operations. We can't use a simple insert statement as the number of rows is 28m.

Does anybody know of a quick way of doing this?

Cheers
0
Comment
Question by:sbarwood
[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
5 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1000 total points
ID: 24852181
I know a couple of approaches. The first two I have used

1) Create a new table with the new column, and do an:

insert into new_tab (col1, ...) select (col1, ...) from old_tab order by col_whatever

Use the APPEND hint to speed it up.

In that step, you'll need to wrap the query in a subquery to do the order by, so you can add the seq.nextval in the outer query.

Like:  insert into newtab select id, newcol, seq.nextval as nextval from (select id, newcol from tab order by id asc);

Advantages: Fastest, least impact to existing table.
Disadvantage: Must switch tables and associated indexes/constraints/views will be affected.


2) Write a PL/SQL procedure, either using bulk or standard processing. Update the rows as you see them. (You said you did not want that route).

Simplest but probably slowest.



3) I don't think you can directly update the table with the sequence value, because of the limitation in Oracle where we cannot use a sequence in combination with order by. Even wrapping in an inline view does not solve it in this case. As such, I never achieved a solution with this approach, but it could be that I just did not try hard enough. We have always used PL/SQL approach unless there were no major dependencies on the original table, in that case the create new table approach works.




0
 
LVL 32

Expert Comment

by:awking00
ID: 24853321
See attached.
update.txt
0
 

Author Closing Comment

by:sbarwood
ID: 31603373
Thanks mrjoltcola, I implemented the first solution and as we are carrying out the changes over downtime the moving and tables etc isnt a problem.
0
 
LVL 32

Expert Comment

by:awking00
ID: 24904054
Did you try my proposal?
0
 

Expert Comment

by:remcogoris
ID: 25288946
Not quite sure what you did and did not want, but this works fast enough, I am sure:

K41> create table test( id integer, kol1 varchar2( 30 ) );
 
Table created.
 
K41> insert into test( id, kol1 ) values( 1, 'kol1a' );
 
1 row created.
 
K41> ed
Wrote file afiedt.buf
 
  1* insert into test( id, kol1 ) values( 2, 'kol1b' )
K41> /
 
1 row created.
 
K41> ed
Wrote file afiedt.buf
 
  1* insert into test( id, kol1 ) values( 5, 'kol1c' )
K41> /
 
1 row created.
 
K41> ed
Wrote file afiedt.buf
 
  1* insert into test( id, kol1 ) values( 4, 'kol1d' )
K41> /
 
1 row created.
 
K41> commit;
 
Commit complete.
 
K41> alter table test
  2  add ( seq integer );
 
Table altered.
 
K41> create sequence t_seq;
 
Sequence created.
 
K41> begin
  2    for rij in ( select * from test order by kol1 )
  3    loop
  4       update test
  5          set seq = t_seq.nextval
  6         where id = rij.id;
  7    end loop;
  8    commit;
  9  end;
 10  /
 
PL/SQL procedure successfully completed.
 
K41> select * from test
  2  order by kol1;
 
        ID KOL1                                  SEQ
---------- ------------------------------ ----------
         1 kol1a                                   1
         2 kol1b                                   2
         5 kol1c                                   3
         4 kol1d                                   4
 
4 rows selected.
 
K41> 

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

718 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