Sunday, 9 July 2017

Load CSV File into Oracle Database Table using PL/SQL

Here I am going to show how to use UTL_FILE package and read the CSV from the Oracle directory and load into the target table.

Step1: Create Target Table Ex: "XX_EMP_TIME_OTL" add the fields as per your requirements.

Step2: Prepare the .CSV File as per the above table columns and save it in a Oracle directory.

>> create the excel sheet and save as <<XX_FILE.csv>>

Step3: Put the CSV file into the Oracle directory and if you do not have any directory then create it as like below:

CREATE OR REPLACE DIRECTORY ORA_DIR as '/oraebs/CLONE/apps/apps_st/appl' 
>>>----- Change the path as per your requirements -- this is oracle directory in the server<<<<<


Create the grants for the user, you are executing the  UTL_FILE:

GRANT READ, WRITE ON DIRECTORY ORA_DIR TO << Your User >>;

Step4:  Check if the user is having UTL_FILE privileges or not :

SELECT OWNER,
OBJECT_TYPE
FROM ALL_OBJECTS
WHERE OBJECT_NAME = 'UTL_FILE'
AND OWNER =<Your User>;

If the user is not having the privileges then grant “UTL_FILE” to user from SYS user:

GRANT EXECUTE ON UTL_FILE TO <Your User>;


Step 5:  Create the Procedure

CREATE or REPLACE PROCEDURE XX_LOAD_CSV
   AS
      F            UTL_FILE.FILE_TYPE;
      V_LINE       VARCHAR2 (1000);
      V_EMPNO      VARCHAR2 (30);
      V_DATE       VARCHAR2 (20);
      V_TIME_IN    VARCHAR2 (20);
      V_TIME_OUT   VARCHAR2 (20);
      v_error      VARCHAR2 (3000);
      v_count      NUMBER;
   BEGIN
      F := UTL_FILE.FOPEN ('ORA_DIR', 'time.csv', 'R');

      IF UTL_FILE.IS_OPEN (F)
      THEN
         LOOP
            BEGIN
               UTL_FILE.GET_LINE (F, V_LINE, 1000);

               IF V_LINE IS NULL
               THEN
                  EXIT;
               END IF;

               V_EMPNO :=
                  REGEXP_SUBSTR (V_LINE,
                                 '[^,]+',
                                 1,
                                 1);
               V_DATE :=
                  REGEXP_SUBSTR (V_LINE,
                                 '[^,]+',
                                 1,
                                 2);
               V_TIME_IN :=
                  REGEXP_SUBSTR (V_LINE,
                                 '[^,]+',
                                 1,
                                 3);
               V_TIME_OUT :=
                  REGEXP_SUBSTR (V_LINE,
                                 '[^,]+',
                                 1,
                                 4);

               INSERT INTO XX_EMP_TIME_OTL
                 VALUES   (V_EMPNO,
                           V_DATE,
                           V_TIME_IN,
                           V_TIME_OUT,
                           'OTL Regular Hours',
                           NULL,
                           NULL,
                           NULL,
                           0,
                           SYSDATE,
                           NULL);

               COMMIT;
            EXCEPTION
               WHEN NO_DATA_FOUND
               THEN
                  EXIT;
            END;
         END LOOP;

         DBMS_OUTPUT.put_line ('Insert Statement Completed Successfully:');

         BEGIN
            SELECT   COUNT ( * ) INTO v_count FROM XX_EMP_TIME_OTL;

            DBMS_OUTPUT.put_line (
               'Total Records Inserted into Table:' || v_count
            );

            INSERT INTO XX_IMPORT_HIST_CSV
              VALUES   ('TIME.CSV' || '_' || SYSDATE,
                        'XX_EMP_TIME_OTL',
                        v_count,
                        SYSDATE);

            COMMIT;
         END;
      END IF;

      UTL_FILE.FCLOSE (F);
   EXCEPTION
      WHEN OTHERS
      THEN
         v_error := SQLERRM;
         DBMS_OUTPUT.put_line ('Exception Error:' || v_error);

         INSERT INTO XX_CSV_LOG
           VALUES   ('XX_EMP_TIME_OTL',
                     V_ERROR,
                     'TIME_ENTRY',
                     SYSDATE);

         COMMIT;

   END XX_LOAD_CSV;


Step6: Execute the procedure.  Your data from the csv file is inserted into the target table in oracle database.