Solved

Duplicate/Replicate data in table

Posted on 2014-02-04
5
546 Views
Last Modified: 2014-02-05
I have a table of 10,000 rows that I need to multiply into 200,000. I basically want to take the data I have in the table duplicate it until there are 200,000 rows, but the empl id needs to be unique so I plan on using a sequence to create it.  Basic table structure below

Case

Emplid    case      case_desc
1             open     this case is open
2             closed   this case is closed
3             open     this case is open

Should I justinsert into case a (select  seq.nxtval, case, case_desc  from case b); until I reach 200K or is this a more efficient way.
0
Comment
Question by:klpayton
[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
5 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39833151
so, simply multiply the data by 20?
Try this...

change 11000  to be any number at least as big as the current max id


INSERT INTO yourtable(emplid,case,case_desc)
    SELECT 11000 + ROWNUM, CASE, case_desc
      FROM yourtable,
           (    SELECT LEVEL
                  FROM DUAL
            CONNECT BY LEVEL <= 20)
where rownum <= 200000
0
 
LVL 32

Expert Comment

by:awking00
ID: 39833170
Is emplid currently a unique key?
0
 

Author Comment

by:klpayton
ID: 39833177
@awking00 Yes, emplid is a unique key
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39833185
the insert above will create 200000 unique new records,  So, if you have 10000 to start with you'll have 210000 when done.

If that's too many, Change the connect by to be   <= 19


or simply delete the extras when you're done
if you don't have 10000 rows to start, then bump the connect by up to 21 or higher as needed.

I would expect the insert above should take a few seconds at most.  Try it, if it doesn't work, simply do a rollback, if it does, then commit and you're done.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39833225
if you'd prefer, the insert can be entirely self-contained by doing a lookup on the max id and counting the current number of rows and adjusting the output to make sure it's exactly 200000 rows when done

This does assume you have between 1 and 199,999 rows to start with though

INSERT INTO yourtable
    WITH x AS (SELECT MAX(emplid) maxid, COUNT(*) cnt FROM yourtable)
    SELECT maxid + ROWNUM, CASE, case_desc
      FROM yourtable,
           x,
           (    SELECT LEVEL
                  FROM DUAL
            CONNECT BY LEVEL <= FLOOR(200000 / (SELECT cnt FROM x)))
     WHERE ROWNUM <= 200000 - cnt;
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

630 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