Balakumaran
Balakumaran

Reputation: 1

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

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;

Upvotes: 0

Views: 10828

Answers (3)

Rob Heusdens
Rob Heusdens

Reputation: 155

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.

Upvotes: 0

A.B.Cade
A.B.Cade

Reputation: 16905

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

Upvotes: 1

cagcowboy
cagcowboy

Reputation: 30848

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

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

Upvotes: 0

Related Questions