简体   繁体   English

从Oracle假脱机处理数据时出现问题。 它将经过的时间假脱机,而不是将sql查询的结果假脱机到输出文件

[英]Problems while spooling data from Oracle. It spools elapsed time instead of spooling result of sql query into output file

I am trying to make a cron job that runs once a day. 我试图做一份每天运行一次的Cron工作。 On invoking the shell script, it calls a Sql file. 在调用shell脚本时,它会调用一个Sql文件。 Which in turn Spools data into a file. 依次将数据假脱机到文件中。 This file is Picked by Shell Script and then mailed accordingly. 该文件由Shell脚本选择,然后相应地邮寄。 Problem is when I try to spool data , it is writing elapsed time instead of writing actual result of query to the output file. 问题是当我尝试假脱机数据时,它正在写入经过的时间,而不是将查询的实际结果写入输出文件。

Here is the sql file I am using. 这是我正在使用的sql文件。

set define off
set numformat 99999999999999999999999999
set markup html on
set serveroutput on
set head on
set pages 3000
set echo off 

DECLARE
total integer :=0;
total = select count(*) from t_c_table1 vt, t_c_table2 ti WHERE vt.f_item_id = ti.f_item_id (+) AND (f_update_date < sysdate - 30)order by F_INSERT_DATE desc; 

IF total > 0 then 

spool /home/output.csv
select f_name, count (*) from t_c_table1 where F_INSERT_DATE < sysdate-100 group by f_item_provider_id;
spool off

END IF

I get output like Elapsed: 00:00:00.506 in the spooled csv file. 在假脱机的csv文件中,我得到的输出类似于Elapsed:00:00:00.506。

Where am i going wrong? 我要去哪里错了? Please Help. 请帮忙。 Thanks in Advance.. 提前致谢..

Code you posted is wrong, it won't even compile in Oracle so I'm surprised that you got anything at all. 您发布的代码是错误的,它甚至无法在Oracle中编译,因此我很惊讶您什么都没有。

As there's no SET TIMING ON , I'm not sure what produced the elapsed time line in the spooled file. 由于没有SET TIMING ON ,所以我不确定是假脱机文件中产生了经过时间线的原因。 Maybe it is some old, previously created CSV file you're looking at? 可能是您正在查看的旧的,以前创建的CSV文件?

Apart from the fact that SPOOL is a SQL*Plus command (so you can't invoke it in a PL/SQL procedure), the way you calculated the TOTAL variable's value is wrong - it should be part of the SELECT ... INTO statement. 除了SPOOL是SQL * Plus命令(因此您不能在PL / SQL过程中调用)之外,计算TOTAL变量值的方式是错误的-它应该是SELECT ... INTO声明。

SQL> declare
  2    total integer := 0;
  3  begin
  4    select count(*)
  5      into total
  6      from dept;
  7
  8    if total > 0 then
  9       spool test.csv
 10       select * from dept;
 11       spool off;
 12    end if;
 13  end;
 14  /
     spool test.csv
           *
ERROR at line 9:
ORA-06550: line 9, column 12:
PLS-00103: Encountered the symbol "TEST" when expecting one of the following:
:= . ( @ % ;


SQL>

If you want to spool data conditionally, you'll have to use the UTL_FILE package. 如果要有条件地后台处理数据,则必须使用UTL_FILE包。

Or, you could do it "interactively" so that SQL*Plus asks you whether you want to spool data or not, as Alex Poole described in his answer here . 或者,您可以“交互地”执行此操作,以便SQL * Plus询问您是否要假脱机数据,如Alex Poole在此处的回答中所述。

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

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