how do I read a local file in SQL Developer?

user786149 picture user786149 · Jun 6, 2011 · Viewed 17.9k times · Source

I want to read a file on my local machine that contains query parameters when I execute a query in Oracle SQL developer. The examples that I've found on the web so far are inadequate. I keep getting "ORA-29283: invalid file operation" errors when I execute the below code:

CREATE DIRECTORY SAMPLEDATA2 AS 'C:';
GRANT READ, WRITE ON DIRECTORY SAMPLEDATA2 TO PUBLIC;


declare
f utl_file.file_type;
s varchar2(200);
c number := 0;

BEGIN

f := utl_file.fopen('SAMPLEDATA2','sample2.txt','R');
loop
    utl_file.get_line(f,s);
    dbms_output.put_line(s);
    c := c + 1;
end loop;

exception
    when NO_DATA_FOUND then
        utl_file.fclose(f);
        dbms_output.put_line('Number of lines: ' || c);
end;

Answer

Justin Cave picture Justin Cave · Jun 6, 2011

UTL_FILE can only read data from files that are stored on the database server. Since it is PL/SQL code, it runs on the database server and only has access to the resources that are available to the Oracle process on the database server.