简体   繁体   English

如何使用PLSQL程序从文本文件插入Oracle 10g中的Table?

[英]How to insert into Table in Oracle 10g from Text file using PLSQL program?

I'm trying this PL/SQL program in Oracle 10g. 我正在Oracle 10g中尝试此PL / SQL程序。 To Read text file (data) from loaction 'C:\\Oracle' and load it into Oracle Table using PLSQL script. 要从位置“ C:\\ Oracle”读取文本文件(数据),并使用PLSQL脚本将其加载到Oracle Table中。 But, I'm getting the following errors: 但是,出现以下错误:

ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 488
ORA-29283: invalid file operation

Here's my Script: 这是我的脚本:

Create or Replace PROCEDURE Rfile is
    line VARCHAR2(100);
    namesfile UTL_FILE.FILE_TYPE;
BEGIN
    --  Syntax : FOPEN ( directory alias, filename, open mode)

    namesfile  := UTL_FILE.FOPEN('FILESDIR1','NAMES2.TXT','R'); -- open in read mode 

    LOOP
      UTL_FILE.GET_LINE(namesfile,line,100);
      dbms_output.put_line(line);

      insert into names2 values(line);                  -- insert into NAMES table
    END LOOP;
EXCEPTION
    WHEN OTHERS THEN
    dbms_output.put_line('Others exceptions....');
END;

You can try a different approach: 您可以尝试其他方法:
Create an external table , which is a table mapped to a file, to read the file and then just insert-select to your table from the external 创建一个外部表 (该表是映射到文件的表)以读取文件,然后从外部表中insert-select到您的表
(or maybe you don't even need to insert it to a different table ?) (或者也许您甚至不需要将其插入到其他表中?)

Your loop does not check for end-of-file, so logically at some point the file read operation should raise an exception, and that is the one you got presumably. 您的循环不检查文件结尾,因此从逻辑上讲,文件读取操作应该引发异常,这大概是您所得到的。

Also you have to check that the directory (FILESDIR1) is pointing to the right OS directory AND you have been granted access to that directory, and the file is on that OS directory (not on your local file system). 另外,您还必须检查目录(FILESDIR1)指向正确的OS目录,并且已授予您对该目录的访问权限,并且该文件位于该OS目录(而不是本地文件系统)上。

Btw. 顺便说一句。 in some cases you could better use SQL Loader to bulk load data in a table, esp. 在某些情况下,您最好使用SQL Loader来批量加载表中的数据,特别是。 if the file is large because you can direct SQL loader to directly load the data in the datafiles, bypassing the SQL layers (generated by the INSERT statements) all together. 如果文件很大,因为您可以指示SQL加载程序直接加载数据文件中的数据,而绕过所有由INSERT语句生成的SQL层。

You may need a lowercase 'r' on this line... 您可能需要在这行上使用小写的“ r” ...

namesfile  := UTL_FILE.FOPEN('FILESDIR1','NAMES2.TXT','r'); -- open in read mode 
                                                       ^

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

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