beginner in oracle trying to read text file

Abhinav picture Abhinav · Oct 11, 2015 · Viewed 8.9k times · Source

I am a beginner in oracle.

I have been trying to read text file and insert its data in utl_file but unable to do so as I do not understand the following things:

Comma1 := INSTR(f_line, ',' ,1 , 1);
Comma2 := INSTR(f_line, ',' ,1 , 2);
Comma3 := INSTR(f_line, ',' ,1 , 3);
Comma4 := INSTR(f_line, ',' ,1 , 4);
Comma5 := INSTR(f_line, ',' ,1 , 5);
f_empno := to_number(SUBSTR(f_line, 1, Comma1-1));
f_ename := SUBSTR(f_line, Comma1+1, Comma2-Comma1-1);
f_job := SUBSTR(f_line, comma2+1, Comma3-Comma2-1);
f_mgr := to_number(SUBSTR(f_line, comma3+1, Comma4-Comma3-1));
f_hiredate := to_date(SUBSTR(f_line, comma4+1, Comma5-Comma4-1),'dd-mon-yyyy');
f_sal := to_number(SUBSTR(f_line, comma5+1),'99999');
dbms_output.put_line(f_empno ||' '|| f_ename || ' ' || f_job || ' ' || f_mgr || ' ' || f_hiredate || ' ' || f_sal);
insert into emp12 VALUES (f_empno,f_ename,f_job,f_mgr,f_hiredate,f_sal);

Answer

Yaron Idan picture Yaron Idan · Oct 12, 2015

This is a nice simple example for the code to read data from a text file, and its much more simple than what you posted originally - http://nimishgarg.blogspot.co.il/2013/04/load-csv-file-in-oracle-using-plsql.html

Here are the relevant pieces of code from the link attached - Run all of these commands in order to read the file EMP_DEPT.CSV from directory e:\mycsv\ into table emp_dept on user scott (change each of these parameters to fit your design)

// 1. First, create a directory object so oracle can access the file's location

create or replace directory MYCSV as 'e:\mycsv\';

// 2. Grant the user reading from the file privileges on that directory

grant read, write on directory MYCSV to scott;

// 3. Create a table in the same structure as the rows in the file

CREATE TABLE EMP_DEPT
  (
  EMPNO NUMBER(4),
  ENAME VARCHAR2(10),
  SAL NUMBER(7,2),
  DNAME VARCHAR2(14)
  );

// 4. Run this procedure after making the following changes - 
// a. Change the variables to match the columns in your destination table
// b. Change the UTL.FILE.FOPEN command to match the directory and file name in your case.
// c. Change every REGEXP_SUBSTR(V_LINE, '[^,]+', 1, 1); to match the variable you want to assign.
// 5. Go for it.
 DECLARE
    F UTL_FILE.FILE_TYPE;
    V_LINE VARCHAR2 (1000);
    V_EMPNO NUMBER(4);
    V_ENAME VARCHAR2(10);
    V_SAL NUMBER(7,2);
    V_DNAME VARCHAR2(14);
  BEGIN
    F := UTL_FILE.FOPEN ('MYCSV', 'EMP_DEPT.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_ENAME := REGEXP_SUBSTR(V_LINE, '[^,]+', 1, 2);
          V_SAL := REGEXP_SUBSTR(V_LINE, '[^,]+', 1, 3);
          V_DNAME := REGEXP_SUBSTR(V_LINE, '[^,]+', 1, 4);
          INSERT INTO EMP_DEPT VALUES(V_EMPNO, V_ENAME, V_SAL, V_DNAME);
          COMMIT;
        EXCEPTION
        WHEN NO_DATA_FOUND THEN
          EXIT;
        END;
      END LOOP;
    END IF;
    UTL_FILE.FCLOSE(F);
  END;
  /

Good luck.