简体   繁体   English

读取文本文件以将数据插入Oracle SQL表

[英]Read text file to insert data into Oracle SQL table

I am studying Oracle SQL developer. 我正在研究Oracle SQL开发人员。

What I am doing is reading text file line by line from the folder. 我正在做的是从文件夹中逐行读取文本文件。 Then Inserting data to the SQL table. 然后将数据插入SQL表。

I am able to compile my PROCEDURE, however, it doesn't seem to be inserting the data to file. 我能够编译我的PROCEDURE,但是,它似乎没有将数据插入文件。

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 我有一个表DATAINSERT与两个varchar(200)类型cols

I am not really sure the reasons that PROCEDURE is not inserting data to table 我不确定PROCEDURE没有向表中插入数据的原因

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

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 - 参考网站 - 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;

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 您必须使用以下sql查询创建目录,而不是直接输入路径

CREATE or replace DIRECTORY USER_DIR AS 'C:\\PROJECTS\\'; 创建或替换DIRECTORY USER_DIR作为'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');

First procedure trying to load the file from PC into the server whereas in second procedure file is from server to server. 尝试将文件从PC加载到服务器的第一个过程,而在第二个过程文件中是从服务器到服务器。 UTL_FILE.FOPEN works for server-side PL/SQL. UTL_FILE.FOPEN适用于服务器端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. 因此,第一个程序将无法正常执行...如果要将文件从PC加载到服务器,则需要使用任何前端,如D @ K或VB。

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. 解决此问题的最快方法是使用WINSCP命令。

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. 使用WINSCP告诉您目录路径是什么,然后只需将此新信息替换为代码中的旧路径,一切都应该正常。

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 您可以在下面找到更多概念和详细信息: 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; 

/ 

first login with 首次登录

username:sys as sysdba username:sys as sysdba

password should be the same as used for user 'system' 密码应与用户'系统'相同

now enter 现在进入

SQL> grant execute on UTL_FILE to PUBLIC; SQL>将UTL_FILE上的执行授予PUBLIC;

Now login with any user with which you want to create procedure 现在使用您要创建过程的任何用户登录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM