我试图在导出为csv的文件名中实现以下要求
filename:filename_yyyymmdd_nnnnnn_xxxxxx.csv
其中
yyyymmdd=生成帐户文件的日期戳
nnnnnnn=6字节随机生成的批处理ID
xxxxxx=6字节零填充字符串,包含文件中的记录数
到目前为止,我已经将以下查询保存为在sqlplus上运行的脚本。
set head off;
set feedback off;
set term off;
set pagesize 0;
set linesize 3000;
set trimspool on;
set colsep ,;
set verify off;
set echo off;
ALTER SESSION SET NLS_DATE_FORMAT= 'MM-DD-YYYY';
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
column date_stamp new_value sys_date noprint
column rnd_num new_value random noprint
column row_count new_value rc noprint
select to_char(sysdate,'mmddYYYY') date_stamp
from dual;
select lpad(round(dbms_random.value(1,999999)),6,'0') rnd_num from dual;
/* select count(*) as row_count
from table a, table b join on a.id = b=id where condition; */
spool filepath.&sys_date..&random..&rc..&1..csv
select statement fetching the actual data;
spool off;
exit;
问题是:我可以生成随机no,但不能添加记录计数(xxxxxx是文件名要求的一部分(。当注释的部分被取消注释并运行时,sql脚本会运行,但不会生成任何文件。当它以";选择计数(*(";如注释所示,文件按预期生成,具有随机无位(nnnnnn(,但没有记录数(xxxxxx(,显然是因为它被注释掉了如何在文件名中也包含记录数
因此,通过对网络的足够阅读,我找到了解决方案。出于某种原因,在我的查询中,row_count返回的值是一个填充了5个空格的值,然后是实际的row_count。张贴以下答案:
set head off;
set feedback off;
set term off;
set pagesize 0;
set linesize 3000;
set trimspool on;
set colsep ,;
set verify off;
set echo off;
ALTER SESSION SET NLS_DATE_FORMAT= 'MM-DD-YYYY';
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
column date_stamp new_value sys_date noprint
column rnd_num new_value random noprint
column row_count new_value rc noprint
select to_char(sysdate,'mmddYYYY') date_stamp
from dual;
select lpad(round(dbms_random.value(1,999999)),6,'0') rnd_num from dual;
select row_count from (select trim(count(*)) as row_count
from table a ...;
spool filepath.&sys_date..&random..&rc..&1..csv
select statement fetching the actual data;
spool off;
exit;