简体   繁体   English

通过使用过程将表导出到csv文件(带时间戳的csv名称)

[英]Export table to csv file by using procedure (csv name with timestamp)

I want to export one table to each csv file and name the csv file with timestamp. 我想将一个表导出到每个csv文件,并用时间戳命名该csv文件。 For example, if I have a table t1 , after export, a csv file name t1.YYYYMMDDHHMISS.csv is generated. 例如,如果我有一个表t1 ,则在导出后,将生成一个csv文件名t1.YYYYMMDDHHMISS.csv。 Here is my code: 这是我的代码:

create or replace procedure 
T_to_CSV(Tname varchar2,Directory varchar2)

BEGIN

set colsep ,     -- separate columns with a comma
set pagesize 0   -- No header rows
set trimspool on -- remove trailing blanks
set headsep off  -- this may or may not be useful...depends on your 

spool timestamp.csv --don't know how to write in directory

select *
  from Tname

end

Here are my questions: 这是我的问题:

(1) I don't know how to output the csv file into the requried directory, how to fix the spool code please? (1)我不知道如何将csv文件输出到需要的目录中,请如何解决假脱机代码?

Should I use spool D:\\path\\filename.csv please? 我应该使用假脱机D:\\path\\filename.csv吗?

(2) In the spool line, how to change the name of the csv file using the timestamp now() please? (2)在假脱机行中,如何使用时间戳now()更改csv文件的名称?

There are a few steps: 有几个步骤:

  • create a directory using `CREATE DIRECTORY my_dir as 'C:\\dir'; 创建一个目录使用`创建目录MY_DIR为“C:\\ DIR”;
  • make sure that Oracle has read,write on the folder on the computer (best accomplished by creating it in the Oracle install folder) 确保Oracle已在计算机上的文件夹上进行读写操作(最好通过在Oracle安装文件夹中创建文件夹来完成此操作)
  • grant the user executing the procedure GRANT read,write on DIRECTORY my_dir to the_user; 授予执行程序GRANT read,write on DIRECTORY my_dir to the_user;的用户GRANT read,write on DIRECTORY my_dir to the_user;
  • download and compile the handy procedure here 在此处下载并编译便捷程序

I have used this and it works really nicely. 我已经用过了,效果很好。

Usage 用法

data_dump ( 'Select emp_name from emp',
             CURRENT_TIMESTAMP||'filename.csv',
             my_dir);

(vastly simplified sample!) (大大简化了示例!)

After creating the directory verify your work by running this: 创建目录后,通过运行以下命令验证您的工作:

  • Select * from ALL_DIRECTORIES; 从ALL_DIRECTORIES中选择*;
  • you should see your directory 你应该看到你的目录
  • logon to the machine where the database is located and verify the folder path exists and the oracle user has permissions on it. 登录到数据库所在的计算机,并验证文件夹路径是否存在以及oracle用户对其具有权限。 Networked drives are only possible if the user running the oracle service has permissions on that folder 仅当运行oracle服务的用户对该文件夹具有权限时,才可以使用网络驱动器
  • Save and copy file in a directory {file need not be executable} 将文件保存并复制到目录{文件不必是可执行文件}
  • Export ORACLE_HOME, PATH and SID of database instance 导出数据库实例的ORACLE_HOME,PATH和SID
  • Navigate to that directory and execute sqlplus 导航到该目录并执行sqlplus
  • Spool file will be created in the same directory as the .sql file 假脱机文件将与.sql文件创建在同一目录中

     SET COLSEP , SET HEADSEP OFF -- character used when defining a two-line column heading SET TRIMSPOOL ON -- trim trailing spaces from each line SET LINESIZE 32000 -- number of characters to be printed on one line SET WRAP OFF -- truncates lines longer than LINESIZE SET NUMWIDTH 5 -- default width while displaying numbers COLUMN tm new_value iso8601 noprint SELECT to_char(sysdate, 'YYYY-MM-DD') tm FROM dual; spool output_file_&iso8601..csv -- Append new data to spool file: "spool output_file_&iso8601..csv append" SELECT * FROM table_name spool OFF 

Thanks kevin for sharing the procedure and it was very useful for me. 感谢kevin分享了该过程,这对我非常有用。 I have customized the code: 我已自定义代码:

  1. To Add the column names in the output csv file which was not working earlier. 要在输出csv文件中添加以前不起作用的列名。
  2. When i was passing the delimiter as parameter then it was adding the comma in the starting of every row(,1,2,3) which has been corrected. 当我将定界符作为参数传递时,它将在已更正的每行(,1,2,3)的开头添加逗号。

I am also sharing the customized code which might help others. 我还分享了可能对他人有帮助的自定义代码。 Customized code can be downloaded here . 定制代码可在此处下载。

  1. Customized Code to add column names 自定义代码以添加列名
  FOR i IN t_describe.FIRST .. t_describe.LAST LOOP IF i <> t_describe.LAST THEN put('UTL_FILE.PUT(v_fh,'''||t_describe(i).col_name||'''||'''||v_delimiter||''');'); ELSE put(' UTL_FILE.PUT(v_fh,'''||t_describe(i).col_name||''');'); END IF; END LOOP; put(' UTL_FILE.NEW_LINE(v_fh);'); 
  1. Customized Code for delimiter 分隔符的定制代码

    IF i <> t_describe.LAST THEN 如果我<> t_describe.LAST THEN
    put(' UTL_FILE.PUT(v_fh,"'||t_describe(i).col_name||'"(i) ||'''||v_delimiter||''');'); put('UTL_FILE.PUT(v_fh,“'|| t_describe(i).col_name ||'”(i)||''|| v_delimiter ||'');');;
    ELSE 其他
    put(' UTL_FILE.PUT(v_fh,"'||t_describe(i).col_name||'"(i));'); put('UTL_FILE.PUT(v_fh,“'|| t_describe(i).col_name ||'”(i));');
    END IF; 万一;

And the correct way to call the procedure is to bind the variable with values 调用该过程的正确方法是将变量与值绑定

data_dump(query_in => 'Select 1 from dual',file_in => 'file.csv',directory_in => 'MY_DIR', delimiter_in => '|' ); data_dump(query_in =>'从对偶中选择1',file_in =>'file.csv',directory_in =>'MY_DIR',delimiter_in =>'|');

Thanks 谢谢

Naveen 纳文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM