Invalid Directory path while executing a procedure that produces a csv file in Oracle (in windows)

14.5k views Asked by At

I have a procedure

create or replace procedure dump_table_to_csv( p_tname in varchar2,
  2                                                 p_dir   in varchar2,
  3                                                 p_filename in varchar2 )
  4  is
  5      l_output        utl_file.file_type;
  6      l_theCursor     integer default dbms_sql.open_cursor;
  7      l_columnValue   varchar2(4000);
  8      l_status        integer;
  9      l_query         varchar2(1000)
 10                      default 'select * from ' || p_tname;
 11      l_colCnt        number := 0;
 12      l_separator     varchar2(1);
 13      l_descTbl       dbms_sql.desc_tab;
 14  begin
 15      l_output := utl_file.fopen( p_dir, p_filename, 'w' );
 16      execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' 
';
 17  
 18      dbms_sql.parse(  l_theCursor,  l_query, dbms_sql.native );
 19      dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
 20  
 21      for i in 1 .. l_colCnt loop
 22          utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' 
);
 23          dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
 24          l_separator := ',';
 25      end loop;
 26      utl_file.new_line( l_output );
 27  
 28      l_status := dbms_sql.execute(l_theCursor);
 29  
 30      while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
 31          l_separator := '';
 32          for i in 1 .. l_colCnt loop
 33              dbms_sql.column_value( l_theCursor, i, l_columnValue );
 34              utl_file.put( l_output, l_separator || l_columnValue );
 35              l_separator := ',';
 36          end loop;
 37          utl_file.new_line( l_output );
 38      end loop;
 39      dbms_sql.close_cursor(l_theCursor);
 40      utl_file.fclose( l_output );
 41  
 42      execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
 43  exception
 44      when others then
 45          execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
 46          raise;
 47  end;
 48  /

When I try to execute

exec dump_table_to_csv('emp', 'C:\Temp','a.txt')

I am getting the error (Error): Error executing statement: ORA-29280: invalid directory path ORA-06512: at "LCM_EVAL.DUMP_TABLE_TO_CSV", line 59 ORA-06512: at line 2

What is the possible problem?

3

There are 3 answers

0
Gary Myers On

Anything to do with UTL_FILE happens on the database server, not the client. Make sure the directory exists on the server, and that the Oracle user has permission to write to it.

0
Vincent Malgrat On

The error message is pretty clear: the path is invalid. Most probably it is just a typo: C:/Temp doesn't exist, but maybe C:\Temp does.

It could also be a problem with grants. In PL/SQL you only have access to the disk if you have been given a specific right. The UTL_FILE documentation explains how to grant access to PL/SQL:

  • (recommended) Create a directory with a dba account, grant access to user.
  • (obsolete) Use the UTL_FILE_DIR system parameter. Be careful, this is a system-wide parameter, all users will be able to write and read this directory.
0
Jeffrey Kemp On

You should create an Oracle DIRECTORY object first, then refer to that object in the call to fopen instead of passing the filesystem path.

More info: http://www.orafaq.com/wiki/Oracle_directory