My table is :
EmpNo Ename Job MGR HireDate Sal
1 ABC DPS JOO 01-10-2002 2000
2 BCD LPS KOL 03-11-2005 2300
3 KOLD MPS BOR 13-03-2012 2400
I am trying to import above table from csv file into pre defined table in oracle database. Below is the code(pl sql code) to import above table.
Code :
Drop table emp_tbl
create table emp_tbl
( EmpNo Number,
EName varchar2(15),
Job Varchar2(15),
MGR Varchar2(30),
HireDate Date,
Sal Number
);
create or replace procedure read_file_proc
Is
f_line varchar2(2000);
f utl_file.file_type ;
f_dir varchar2(250);
fname varchar2(50);
comma1 Number;
comma2 Number;
comma3 Number;
comma4 Number;
comma5 Number;
f_empno emp_tbl.empno%type;
f_ename emp_tbl.ename%type;
f_job emp_tbl.job%type;
f_mgr emp_tbl.MGR%type;
f_hiredate emp_tbl.hiredate%type;
f_sal emp_tbl.sal%type;
Begin
dbms_output.put_line('At Begin');
fname := 'Emp_tbl.csv';
f := utl_file.fopen('UTL_FILE_DIR', fname, 'R');
dbms_output.put_line('Before loop');
loop
Begin
Utl_file.get_line(f, f_line);
--dbms_output.put_line(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 := 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));
insert into emp_tbl values(f_empno, f_ename, f_job, f_mgr, f_hiredate, f_sal);
end loop;
utl_file.fclose(f);
--commit;
end read_file_proc;
But upon running in sql developer i am receiving below error :
ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at "SYSTEM.READ_FILE_PROC", line 36 ORA-06512: at line 2
But when i am entering values directly(by using insert query) for f_empno, f_hiredate, f_salary... it is working fine.
Can anybody please tell me Why i am getting above mentioned error.
Thanks in advance.
EDIT : As asked the first row in CSV file appears as :
1,Akon,TSP,Navi,01-10-2002,2000