Read text file to insert data into Oracle SQL tabl

2019-02-07 09:10发布

I am studying Oracle SQL developer.

What I am doing is reading text file line by line from the folder. Then Inserting data to the SQL table.

I am able to compile my PROCEDURE, however, it doesn't seem to be inserting the data to file.

Create or Replace PROCEDURE Rfile is
f UTL_FILE.FILE_TYPE;
s VARCHAR2(200); 
BEGIN
f := UTL_FILE.FOPEN('C:\Projects\','testdatabinary.txt','R');
  IF UTL_FILE.IS_OPEN(f) THEN
    LOOP
      BEGIN
        UTL_FILE.GET_LINE(f,s);
        IF s IS NULL THEN
          EXIT;
        END IF;
      INSERT INTO DATAINSERT
      (COLUMN1, COLUMN2)
      VALUES
      (s, 'testdatabinary');
      END;
  END LOOP;
  COMMIT;
  END IF;
 END;

And I have a table DATAINSERT with two varchar(200) type cols

I am not really sure the reasons that PROCEDURE is not inserting data to table

I just checked error message

Error starting at line 1 in command:
EXEC Rfile
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "SYSTEM.RFILE", line 5

6条回答
ら.Afraid
2楼-- · 2019-02-07 09:19

You can't able to enter the path directly to the file open command like this

f := UTL_FILE.FOPEN('C:\Projects\','testdatabinary.txt','R');

Instead of entering path directly, you have to create directory using the below sql query

CREATE or replace DIRECTORY USER_DIR AS 'C:\PROJECTS\';

Then enter the directory name to the file open command. It will be like this

f := UTL_FILE.FOPEN('USER_DIR ','testdatabinary.txt','R');
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-07 09:20

first login with

username:sys as sysdba

password should be the same as used for user 'system'

now enter

SQL> grant execute on UTL_FILE to PUBLIC;

Now login with any user with which you want to create procedure

查看更多
Animai°情兽
4楼-- · 2019-02-07 09:26

First procedure trying to load the file from PC into the server whereas in second procedure file is from server to server. UTL_FILE.FOPEN works for server-side PL/SQL. So first procedure wont execute properly... If you want to load a file from PC to server, then you need to user any front end like D@K or VB.

查看更多
孤傲高冷的网名
5楼-- · 2019-02-07 09:34

The mistake your making here is using the workstation path, you actually need to be using the directory path.

The quickest way to resolve this, is just to use the WINSCP command.

Use WINSCP to tell you what the directory path is, then simply substitute this new information for the old path you have in your code, and everything should work ok.

查看更多
何必那么认真
6楼-- · 2019-02-07 09:41

Not sure what causing problems. For me its working fine here is my example code


--Reference Site --https://community.oracle.com/thread/3633577?start=0&tstart=0

     set serveroutput on;
     CREATE or replace DIRECTORY USER_DIR AS '/home/oracle'; 
     GRANT READ ON DIRECTORY USER_DIR TO PUBLIC;

     DECLARE 
        V1 VARCHAR2(200); --32767
        F1 UTL_FILE.FILE_TYPE; 
     BEGIN 
        F1 := UTL_FILE.FOPEN('USER_DIR','temp.txt','R'); 
        Loop
        BEGIN
    UTL_FILE.GET_LINE(F1,V1); 
    dbms_output.put_line(V1);
    EXCEPTION WHEN No_Data_Found THEN EXIT; END;
        end loop;

        IF UTL_FILE.IS_OPEN(F1) THEN
     dbms_output.put_line('File is Open');
        end if;

        UTL_FILE.FCLOSE(F1); 
     END; 
     /
    set serveroutput off;

查看更多
等我变得足够好
7楼-- · 2019-02-07 09:43

This is very conceptual and tricky. which you can find more concepts and details below: https://ora-data.blogspot.in/2016/11/read-and-writ-text-file-using-UTLFILE.html

Sure will be helpful:

create or replace procedure read_file is 
f_line varchar2(2000); 
f utl_file.file_type; 
f_dir varchar2(250); 
fname varchar2(50); 
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.hiredate%type; 
f_sal emp.sal%type; 
begin 
f_dir := ‘E:\PLSQL’; 
fname := ‘input.txt’; 
f := utl_file.fopen(‘UTL_FILE_DIR’,fname,’r’); –opening the file using fopen function 
loop 
begin 
utl_file.get_line(f,f_line); 
–using a loop continuously get the file’s content using get_line function 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);
–Each field in the input record is delimited by commas. 
–We need to find the location of two commas in the line. 
–and use the locations to get the field from the line. 
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; 

/ 
查看更多
登录 后发表回答