Copy from one table to another in chunks
900542Dec 5 2011 — edited Dec 5 2011Hi,
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;