PLSQL BETWEEN程序无法正常工作

时间:2016-11-17 23:31:07

标签: sql plsql

我是PL / SQL的新手,我最近创建了一个新程序,似乎可以正常编译,但是当我执行该程序时,它不会显示任何内容。

我想要完成的是选择邮件代码,其中包含两个输入之间受影响的最大房屋数量为outage_start_time timestamp,outage_end_time timestamp。

表: OUTAGE_ID,OUTAGE_START,OUTAGE_END,OUTAGE_ZIPCODE,OUTAGE_STATUS,HOUSES_AFFECTED

程序:

create or replace procedure start(outage_start_time timestamp, outage_end_time timestamp)
as
  Cursor test is 
  select outage_zipcode 
  from outage 
  where HOUSES_AFFECTED in (select max(HOUSES_AFFECTED)from outage)
  and outage_start_time between outage_start and outage_end 
  and outage_end_time  between outage_start and outage_end;
 average varchar(256);
BEGIN
  Open test;
  Loop
    fetch test into average;
    exit when test%notfound;
    dbms_output.put_line(average);
 end loop;
end;

3 个答案:

答案 0 :(得分:0)

考虑到您的评论,我可以假设您的average变量包含NULL值。在SQL * Plus中,它看起来像一个空行。要确保您可以添加输出内容:

dbms_output.put_line('average value is: ' || average);

甚至

dbms_output.put_line('average value is: ''' || average || '''');

当您需要从空格符号中区分NULL值时,最后一个非常好。

由于您的查询没有返回任何行,

average变量可能为null。为什么 - 这是您的数据的问题。

答案 1 :(得分:0)

您的查询不会返回任何行。运行这个:

select outage_start, outage_end
  from outage 
 where HOUSES_AFFECTED in (select max(HOUSES_AFFECTED)from outage)

并检查是否有任何记录,其中传递给您的过程的outage_start_time和outage_end_time都介于outage_start和outage_end之间。我很确定没有这样的记录。请记住,如果outage_start或outage_end为null,则表示不满足您的WHERE条件。

答案 2 :(得分:0)

没有样本数据和预期的结果/输出,几乎不可能知道你真正想要的是什么。

我会首先开发适当的查询。也许这会有所帮助:

SELECT DISTINCT
      outage_zipcode
FROM  (
            SELECT
                  MAX(HOUSES_AFFECTED) over(partition by 1) as MX
                , HOUSES_AFFECTED
                , outage_zipcode
            FROM outage
      AND (    %1 BETWEEN outage_start AND outage_end
            OR %2   BETWEEN outage_start AND outage_end
            )
   )
WHERE HOUSES_AFFECTED = MX
;