Useful shortcuts for vi editor

Friday, 16 February 2018

e.g. bulk insert

SET TIMING ON
/
CREATE TABLE HR.EMPLOYEES_TEST
AS
   SELECT *
     FROM HR.EMPLOYEES
    WHERE ROWNUM < 0;
/
DECLARE
   TYPE TABLE_TYP IS TABLE OF HR.EMPLOYEES%ROWTYPE;

   TABLE_ROWS   TABLE_TYP := TABLE_TYP ();

   CURSOR TEST_CURSOR
   IS
      SELECT * FROM HR.EMPLOYEES;

   V_COUNT      NUMBER;
BEGIN
   SELECT COUNT (1) INTO V_COUNT FROM HR.EMPLOYEES;

   DBMS_OUTPUT.PUT_LINE ('TOTAL NUMBER OF ROWS: ' || V_COUNT);

   OPEN TEST_CURSOR;

   LOOP
      FETCH TEST_CURSOR BULK COLLECT INTO TABLE_ROWS LIMIT 10;

      FORALL A IN TABLE_ROWS.FIRST .. TABLE_ROWS.LAST
         INSERT INTO HR.EMPLOYEES_TEST
              VALUES TABLE_ROWS (A);

      COMMIT;
      DBMS_OUTPUT.PUT_LINE (
         'Looping, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);
      EXIT WHEN TEST_CURSOR%NOTFOUND;
   
   --EXIT WHEN TEST_CURSOR%ROWCOUNT > 1000000;

   END LOOP;

   DBMS_OUTPUT.PUT_LINE (
      'Exited, TEST_CURSOR%rowcount = ' || TEST_CURSOR%ROWCOUNT);

   CLOSE TEST_CURSOR;
END;
/