动态假脱机到文件名

时间:2016-03-29 08:23:20

标签: sql csv plsql sqlplus spool

我想在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;
  1. 我得到一个提示,要求为所有new_vpath,new_presidio ..输入一个值,至少是第一次。如果我删除这部分,我会收到它应该开始假脱机的请求
  2. 如果我使用pl / sql,我可以成功设置变量,因此不会出现提示,但假脱机和查询不起作用。实际假脱机说非法文件名和查询不适用于查询中缺少“account_code”字符串。
  3. 我在哪里以及我做错了什么?

1 个答案:

答案 0 :(得分:0)

可能没什么。这与@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) $