动态假脱机到文件名
Posted
技术标签:
【中文标题】动态假脱机到文件名【英文标题】:Dynamic Spool to File name 【发布时间】:2016-03-29 08:23:20 【问题描述】:我想每周在 sqlplus 或 SQLCL 中提取相同的查询,可能会在 Windows 服务器上安排任务,在 csv 文件中使用帐户代码和日期的名称进行假脱机。 我编写了以下脚本,它可以工作,但我有两个问题。
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
COL VPATH NEW_VALUE NEW_VPATH;
COL PRESIDIO NEW_VALUE NEW_PRESIDIO;
COL SEPARATORE NEW_VALUE NEW_SEPARATORE;
COL DATA NEW_VALUE NEW_DATA;
COL EXT NEW_VALUE NEW_EXT;
SET SQLFORMAT CSV;
SELECT 'C:\Shared_Folder\' VPATH, 'AO_GALLARATE' PRESIDIO,'_' SEPARATORE, TO_CHAR(CURRENT_DATE) DATA,'.CSV' EXT FROM DUAL;
SET FEEDBACK ON;
SET TERMOUT OFF;
SPOOL "&NEW_VPATH&NEW_PRESIDIO&NEW_SEPARATORE&NEW_DATA&NEW_EXT";
SELECT /*CSV*/ SERIAL_NUMBER, DNS_HOST_NAME, IP_ADDRESS, MODEL_NAME, TOTAL_PAGE_COUNT - COLOR_PAGE_COUNT, COLOR_PAGE_COUNT, TRUNC(LAST_DATA_COLLECTION) FROM DEVICE WHERE ACCOUNT_CODE = '&NEW_PRESIDIO' AND model_name LIKE '%MS410%';
ORDER BY SERIAL_NUMBER ASC;
SPOOL OFF;
-
我收到一个提示,要求为所有 new_vpath、new_presidio.. 至少第一次输入一个值。如果我删除这部分,我会在应该开始假脱机时收到请求
如果我使用pl/sql,我可以成功设置变量,所以没有提示出现,但是假脱机和查询不起作用。实际上假脱机表示非法文件名和查询不适用于查询中缺少“account_code”字符串。
我在哪里做错了什么?
【问题讨论】:
ORACLE How to use spool with dynamic spool location的可能重复 【参考方案1】:可能什么都没有。这与@ruudvan 所说的重复有点不同。您应该能够按照您的方式在 spool 命令上使用替换。
这是早期版本中的错误。在最新版本的 SQLcl 中,我已经尝试过了,似乎效果很好。
(~/qa) $cat subnew.sql
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
COL VPATH NEW_VALUE NEW_VPATH;
COL PRESIDIO NEW_VALUE NEW_PRESIDIO;
COL SEPARATORE NEW_VALUE NEW_SEPARATORE;
COL DATA NEW_VALUE NEW_DATA;
COL EXT NEW_VALUE NEW_EXT;
COL USER NEW_VALUE NEW_USER;
SET SQLFORMAT CSV;
SELECT './' VPATH, 'AO_GALLARATE' PRESIDIO,'_' SEPARATORE, TO_CHAR(CURRENT_DATE) DATA,
'.CSV' EXT , USER FROM DUAL;
SET FEEDBACK OFF;
SET TERMOUT OFF;
SPOOL "&NEW_VPATH&NEW_PRESIDIO&NEW_SEPARATORE&NEW_DATA&NEW_EXT";
select /* csv */ * from substitution
where upper(name) = '&NEW_USER';
SPOOL OFF;
exit
(~/qa) $sql barry/oracle @subnew
SQLcl: Release 12.2.0.1.0 RC on Tue Aug 09 13:21:28 2016
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Last Successful login time: Tue Aug 09 2016 13:21:29 +01:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
"VPATH","PRESIDIO","SEPARATORE","DATA","EXT","USER"
"./","AO_GALLARATE","_","09-AUG-16",".CSV","BARRY"
(~/qa) $ls -altr AO_GALLARATE_09-AUG-16.CSV
-rw-r--r-- 1 bamcgill staff 22 9 Aug 13:21 AO_GALLARATE_09-AUG-16.CSV
(~/qa) $cat AO_GALLARATE_09-AUG-16.CSV
"ID","NAME"
1,"Barry"
(~/qa) $
【讨论】:
以上是关于动态假脱机到文件名的主要内容,如果未能解决你的问题,请参考以下文章
使用 SQL Developer (SQLcl) 假脱机到动态位置