0

I'm trying this PL/SQL program in Oracle 10g. To Read text file (data) from loaction 'C:\Oracle' and load it into Oracle Table using PLSQL script. 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;
Burhan Ali
  • 2,258
  • 1
  • 28
  • 38
Balakumaran
  • 1
  • 1
  • 1
  • 1
  • You've created a DIRECTORY i assume? – cagcowboy Mar 28 '12 at 06:59
  • See my response to this other question on the topic: http://stackoverflow.com/questions/2751113/utl-file-fopen-procedure-not-accepting-path-for-directory/2753582#2753582 – APC Mar 28 '12 at 17:51
  • 1
    Please, Refer the below link for more details(directory and all..) : http://srikanthtechnologies.com/blog/utl_file.html – Balakumaran Mar 30 '12 at 11:09

3 Answers3

1

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
(or maybe you don't even need to insert it to a different table ?)

A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
0

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).

Btw. in some cases you could better use SQL Loader to bulk load data in a table, esp. 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.

Rob Heusdens
  • 155
  • 5
0

You may need a lowercase 'r' on this line...

namesfile  := UTL_FILE.FOPEN('FILESDIR1','NAMES2.TXT','r'); -- open in read mode 
                                                       ^
cagcowboy
  • 30,012
  • 11
  • 69
  • 93