微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 sqlplus 中使用假脱机向文件名添加额外的数据/信息

如何解决在 sqlplus 中使用假脱机向文件名添加额外的数据/信息

我正在尝试在导出为 csv 的文件名中实现以下要求
文件名:文件名_yyyymmdd_nnnnnn_xxxxxx.csv
哪里
yyyymmdd = 帐户文件生成时的日期戳
nnnnnn = 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;

问题是: 我能够生成随机数,但无法添加记录数(文件名要求的 xxxxxx 部分)。当注释的部分取消注释并运行时,sql脚本运行但不生成文件。当它以注释的“select count(*)”部分运行时,文件按预期生成,具有随机无位(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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。