Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Copy from one table to another in chunks

900542Dec 5 2011 — edited Dec 5 2011
Hi,

I want to copy data from one table to another in chunks. It looks like this code only copies first 5000 records in each loop. How to change the code, so that it copies the exact number of lines from table1 to table2? (Sorry about formatting, I can't find any code markups..)

DECLARE
l_chunk NUMBER := 5000;
l_current_id NUMBER := 0;
l_how_many NUMBER := 0;

BEGIN
SELECT COUNT(id) INTO l_how_many FROM table1;
--
LOOP
--
INSERT INTO table2
SELECT *
FROM table1
WHERE rownum <= l_chunk;

--
l_current_id := l_current_id + l_chunk;
EXIT WHEN l_current_id >= l_how_many;
--
IF ((l_how_many - l_current_id) < l_chunk) THEN
l_chunk := l_how_many - l_current_id;
END IF;
--
END LOOP;
END;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2012
Added on Dec 5 2011
7 comments
927 views