执行在 Oracle 中生成 csv 文件的过程时目录路径无效(在 Windows 中)

发布于 2024-10-19 17:15:22 字数 2305 浏览 3 评论 0原文

我有一个程序

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  /

当我尝试执行时

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

出现错误 (错误):执行语句时出错:ORA-29280:无效的目录路径 ORA-06512:位于“LCM_EVAL.DUMP_TABLE_TO_CSV”,第 59 行 ORA-06512:位于第 2 行

可能的问题是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷清清 2024-10-26 17:15:22

错误消息非常清楚:路径无效。很可能这只是一个拼写错误:C:/Temp 不存在,但也许 C:\Temp 存在。

这也可能是赠款的问题。在 PL/SQL 中,只有在被授予特定权限的情况下,您才可以访问磁盘。 UTL_FILE 文档< /a> 解释如何授予对 PL/SQL 的访问权限:(

  • 推荐)使用 dba 帐户创建目录,向用户授予访问权限。
  • (已过时)使用 UTL_FILE_DIR 系统参数。请注意,这是一个系统范围的参数,所有用户都可以写入和读取此目录。

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.
清风挽心 2024-10-26 17:15:22

您应该首先创建一个 Oracle DIRECTORY 对象,然后在调用 fopen 时引用该对象,而不是传递文件系统路径。

更多信息:http://www.orafaq.com/wiki/Oracle_directory

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

思慕 2024-10-26 17:15:22

与 UTL_FILE 相关的任何事情都发生在数据库服务器上,而不是客户端上。确保服务器上存在该目录,并且 Oracle 用户有权写入该目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文