简体   繁体   English

在 Oracle SQL 运行期间将系统日期添加到文件名

[英]Add System Date to file name during Oracle SQL Run

I am running the following code in SQL developer (oracle) to run the query and export a csv file into a folder.我在 SQL 开发人员(oracle)中运行以下代码来运行查询并将 csv 文件导出到文件夹中。 I would like to add the system date to the filename as well.我也想将系统日期添加到文件名中。 I am using the following code.我正在使用以下代码。 Although it does the job, it asks the user to input the date in the pop up window.虽然它完成了这项工作,但它要求用户在弹出窗口 window 中输入日期。 I am looking to get rid of the pop up window and rather have the code use the system date instead.我希望摆脱弹出 window ,而是让代码使用系统日期。 Is there any way i can eliminate the user input window?有什么办法可以消除用户输入 window?

Second issue is that this also brings in the SQL code to the output file along with the query results, is there any way to avoid bringing in the SQL as well?第二个问题是,这也将 SQL 代码与查询结果一起引入到 output 文件中,有什么方法可以避免引入 SQL 吗?

set VERIFY off 
set FEEDBACK off
set echo off 
set heading off
col date_stp new_value date_stp
Select to_char(sysdate,'yyyymmdd') date_stp from dual;

Spool 'I:\Folder\ExportData&date_stp..csv'; 
SET sqlformat csv;
Select Customer, ID, etc -- the content of the query

Spool off;

Here's how: in order to avoid displaying the select statement within the spooled file, you need to方法如下:为了避免在假脱机文件中显示select语句,您需要

set term off
set feed off

but not directly in SQL*Plus (or SQL Developer);不是直接在 SQL*Plus(或 SQL Developer)中; you'll have to save the following contents into a.SQL file and run it using @ .您必须将以下内容保存到 a.SQL 文件中并使用@运行它。

So: p.sql所以: p.sql

set term off
set feed off

col sd new_value x
select to_char(sysdate,'YYYYMMDD') sd from dual;

spool dept&x..txt
select * From dept;
spool off

Testing:测试:

SQL> @p                                 --> call the .sql file
SQL> $dir *.txt
 Volume in drive C is OSDisk
 Volume Serial Number is 7635-F892

 Directory of C:\Users\lf

12.06.2020.  21:13               494 dept20200612.txt       --> file name is OK
               1 File(s)            494 bytes
               0 Dir(s)  260.040.732.672 bytes free

SQL> $type dept20200612.txt
                                               --> no SELECT statement here
    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL>

In order to stop SQL*Plus or SQL Developer asking for value of a substitution variable (that's what &something represents), run set define off .为了停止 SQL*Plus 或 SQL Developer 请求替换变量的值(这就是&something所代表的),运行set define off

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

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