Reputation: 41
Hi guys IP am trying to use the utl_file package in PLSQL...
I have granted the user to create directory privilege and read and write privilege from sysdba...when I create a directory it says "directory created", but is physical harddrive local location no directory is created.
When I write a code to give output in file it gives some errors.
Code:
SQL> create or replace directory PLSQL as 'D:\PLSQL';
Directory created.
SQL>declare
2 fp utl_file.file_type;
3 begin
4 fp := utl_file.fopen('D:\PLSQL', 'Hello.txt','w');
5 utl_file.putf(fp,'HI THIS IS WRITTEN IN SQLPLUS');
6 utl_file.fclose(fp);
7 end;
8 /
declare
*
ERROR at line 1:
ORA-29280: invalid directory object
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 4
if any solution help...
Upvotes: 0
Views: 659
Reputation: 2210
1st create a logical directory using below syntax:
create directory logfile as 'D:\PLSQL';
and then refer the same in your code. Also please be sure you have read, write grant on this new logical directory.
GRANT READ,WRITE ON DIRECTORY logfile TO user_Name;
declare
2 fp utl_file.file_type;
3 begin
4 fp := utl_file.fopen(logfile , 'Hello.txt','w');
5 utl_file.putf(fp,'HI THIS IS WRITTEN IN SQLPLUS');
6 utl_file.fclose(fp);
7 end;
8 /
Upvotes: 1
Reputation: 142720
Two objections so far:
when you create a directory (an Oracle object), nothing is "automatically" created in filesystem. That directory (D:\PLSQL) has to exist, you have to create it manually (using e.g. Windows Explorer or Command Prompt or ...).
when referencing the directory in PL/SQL code, you have to use its name (PLSQL
), not its path (D:\PLSQL
):
fp := utl_file.fopen('PLSQL', 'Hello.txt','w');
I tested your code, works just fine (file is created in my directory).
Upvotes: 2