Getting conversion error while running a procedure - Oracle SQL

259 Views Asked by At
CREATE OR REPLACE PROCEDURE read_file IS
  f_line     VARCHAR2(2000);
  f          utl_file.file_type;
  comma1     VARCHAR(10);
  comma2     VARCHAR(10);
  comma3     VARCHAR(10);
  comma4     VARCHAR(10);
  comma5     VARCHAR(10);
  f_empno    emp.empno%TYPE;
  f_ename    emp.ename%TYPE;
  f_job      emp.job%TYPE;
  f_mgr      emp.mgr%TYPE;
  f_hiredate emp.doj%TYPE;
  f_sal      emp.sal%TYPE;
  BEGIN
    f := utl_file.fopen('LOG_FILES', 'ab1.txt', 'r');

    LOOP
      BEGIN
        utl_file.get_line(f, f_line);
        EXCEPTION WHEN no_data_found THEN EXIT;
      END;
      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);
    END LOOP;

    utl_file.fclose(f);

    COMMIT;
  END read_file;
  /

If I execute the program in sql command prompt as exec read_file;

I got error message as:

BEGIN read_file; END;

*

ERROR at line 1:

ORA-06502: PL/SQL: numeric or value error

ORA-06512: at "SYSTEM.READ_FILE", line 34

ORA-06512: at line 1
3

There are 3 best solutions below

11
Megh Vidani On

Your question is a bit unclear.

If you want to execute it in SQL command prompt (sqlplus.exe) you can use

exec read_file;

If you want to execute it inside some other program then you need to specify which programming language you're using. The answer will vary depending on that.

0
Lalit Kumar B On

In SQL*Plus:

EXEC[UTE] procedure_name;

To call it within PL/SQL:

BEGIN
   procedure_name;
END;
/
6
Tatiana On
f := utl_file.fopen('UTL_FILE_DIR',fname,'r');

UTL_FILE_DIR should be a directory which you created earlier. For example

CREATE DIRECTORY UTL_FILE_DIR AS 'C:\LOG_FILES';

After it you can use this directory in utl_file.fopen. It will read file from UTL_FILE_DIR directory which is C:\LOG_FILES now.

You should create it only once, before you execute the procedure

And I think you don't need f_dir in that case