查询CURSOR FOR LOOP返回的数据

时间:2017-04-04 09:25:39

标签: oracle plsql

我需要一些以下PL / SQL代码的帮助,这些代码显示两个日期值之间的分钟时差。 IF-Statement之前的第一部分返回了所需的信息,但是当我尝试编写IF语句以显示仅超过15分钟的那些作业的时差时,然后通过以下方式发送电子邮件通知Exchange Server。

提前感谢您的协助。

Declare

l_found boolean :=false;

Cursor C5 

is 
    (SELECT CASE LENGTH(JCSBMTIME) 
     WHEN 0 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('000000',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  
     ,'DD-MON-YY HH24:MI:SS')) * 1440),2)
     WHEN 1 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('00000',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  
     ,'DD-MON-YY HH24:MI:SS')) * 1440),2)
     WHEN 2 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('0000',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  ,'DD-
     MON-YY HH24:MI:SS')) * 1440),2)
     WHEN 3 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('000',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  ,'DD-
     MON-YY HH24:MI:SS')) * 1440),2)
     WHEN 4 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('00',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  ,'DD-
     MON-YY HH24:MI:SS')) * 1440),2)
     WHEN 5 THEN
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('0',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  ,'DD-
     MON-YY HH24:MI:SS')) * 1440),2)
     ELSE
     round(((sysdate - to_date 
     (TO_DATE(TO_CHAR(to_number(JCSBMDATE)+1900000),'YYYYDDD') || ' ' ||  
     to_char(to_date(CONCAT('',JCSBMTIME),'HH24:MI:SS'),'HH24:MI:SS')  ,'DD-MON-
     YY HH24:MI:SS')) * 1440 ),2)
     END AS "DATETIME_DIFF", 
     JCUSER, JCJOBNBR, JCSBMTIME FROM SVM900E1.F986110 
     WHERE JCSBMDATE = (TO_CHAR(sysdate,'YYYYDDD')-1900000) 
     AND JCJOBSTS in ('P','S','W')); 

Begin

    For Y in C5 

Loop 

l_found := true;

    dbms_output.put_line(Y.DATETIME_DIFF||' '||Y.JCUSER||' ' ||Y.JCJOBNBR||' 
    '||Y.JCSBMTIME); 
End loop;

if not l_found

Then

    dbms_output.put_line('No records found');

End if;

Case Y.DATETIME_DIFF  when > 15 

Then 

   dbms_output.put_line(Y.JCUSER||' ' ||Y.JCJOBNBR||' '||Y.JCSBMTIME);

Else 'No records';

End case;

End;

1 个答案:

答案 0 :(得分:0)

注意你的Y迭代器范围。您正试图在循环语句中使用它。

你的代码应该是这样的。我省略了游标声明的一部分      - 首先init为false     l_found:= false;

 for Y in your_cursor loop 
 -- if we are in loop , we are found a record , change value to true
 l_found := true;

  check conditions, do output etc...

 end loop;

-- if cursor have no result 
if not l_found then
    dbms_output.put_line('No records found');
end if;