3

I have to read a text file that I have created on the database. I am not sure what the appropriate path would be for the UTL_FILE.FOPEN directory parameter. I am confused because the path is not on my local pc.

The path where I found this file is something like

\\{something here}\winixdb$\{schema here I think}\dev\data

This is my current code.

Declare

f UTL_FILE.FILE_TYPE;
s VARCHAR2(200);

begin

  f:= UTL_FILE.FOPEN({path_here}, 'certs_file.txt', 'R');
  UTL_FILE.GET_LINE(f,s);
  UTL_FILE.FCLOSE(f);
  dbms_output.put_line(s);

end;

Right now if I put the exact path there it will be an error:

ORA-2928: Invalid Directory Path

I've tried looking at other resources but they talk about a path that is found on your local environment. Also, I don't quite understand the idea of a file on your database.

I want to see this file be outputted to the DBMS.

3
  • 3
    Maybe the answer to this question will help you. directory is a database object you need to create (just like creating a table or an index). The location is on the db server machine, not on the client. Commented Jul 13, 2016 at 18:30
  • can I acces the location from using toad, navigating to schema, directory and finding it there? edit: I did that and put in the exact directory path but I still seem to get errors Commented Jul 13, 2016 at 18:35
  • I'm guessing that the path we're talking about is a UNC path and that the database is running on Windows (correct me if I'm wrong). Are you putting the full UNC path in the fopen call? Is that path in your utl_file_dir setting? Or did you create a directory object that points at that UNC path? Does the operating system user running the Oracle service have access to that UNC path? Commented Jul 13, 2016 at 19:22

1 Answer 1

4

You must first declare the directory you want to access {path_here}:

CREATE OR REPLACE DIRECTORY  MY_PATH_ON_DBMS as '{path_here}';

(if it already exists, you should find it with select * from dba_directories;)

; make sure oracle user can read to it on the DBMS;

Then call it by its name in your code:

Declare
  f UTL_FILE.FILE_TYPE;
  s VARCHAR2(200);
begin
  f:= UTL_FILE.FOPEN('MY_PATH_ON_DBMS', 'certs_file.txt', 'R');
  UTL_FILE.GET_LINE(f,s);
  UTL_FILE.FCLOSE(f);
  dbms_output.put_line(s);
end;
Sign up to request clarification or add additional context in comments.

7 Comments

Should the oracle user have write permission too or is reading permission sufficient?
The read only access should be enough. Thanks I updated this.
Do I have to declare it? I am seeing some packages in other schemas in which it is not declared (or declared just like a variable).
You declare it on the database, not in the package. Check if it already exists with select * from dba_directories;
Yes. I see the name of the directory, V_COP, in there. If I create it in my plsql file I get an insufficient priveldges error but that makes no sense since I can navigate to the directory and read/write the files. Furthermore, I see people reading files without declaring the directory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.