【问题标题】:How to show message depending on presence or absence of records?如何根据记录的存在与否显示消息?
【发布时间】:2021-10-27 02:50:02
【问题描述】:

我想使用存储过程 (Oracle) 生成我的报告。我想显示的是如果程序被执行,我希望报告消息的结尾是““指定年份”的报告结束”,如果根本没有任何数据,我想显示“NO Record for "specified year"”

CREATE OR REPLACE PROCEDURE booking_pending_report(in_month IN NUMBER,in_year IN NUMBER) AS

cursor pending_booking_basedondate_cursor is 
    select bo.journeystatus As "Journey Status",bo.bookingdate As "Booking Date",bo.fromdestination As "Location From",bo.todestination As "Desired Location", co.customerid As "Customer ID", co.customername As "Customer Name", co.customertel AS "Customer Phone No", concat(TRUNC((SYSDATE - TO_DATE(co.customerdob, 'DD-MON-YYYY'))/ 365.25),' years old') as "Customer Age", co.customergender as "Gender"
    from booking bo, customer co
    where bo.journeystatus = 'Pending' and bo.bookingdate between to_date('01-'||in_month||'-'||in_year,'dd-mm-yyyy') AND to_date('30-'||in_month||'-'||in_year,'dd-mm-yyyy') and bo.customerid = co.customerid
    group by bo.journeystatus, bo.fromdestination,bo.todestination, bo.bookingdate, co.customerid, co.customername, co.customertel, co.customerdob, co.customergender
    order by bookingdate;


pending_booking_basedondate_record pending_booking_basedondate_cursor%ROWTYPE;
v_count number(2);
v_countforchecking number(2);

BEGIN

DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------');
DBMS_OUTPUT.PUT_LINE(rpad('.',30)||rpad('ON DEMAND REPORT',34)); 
DBMS_OUTPUT.PUT_LINE(rpad('.',6)||'Booking that are still on "Pending" in year '||in_year); 
DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------');


OPEN pending_booking_basedondate_cursor;
v_count:=1;
v_countforchecking :=0;

Loop
 fetch pending_booking_basedondate_cursor into pending_booking_basedondate_record;

  if pending_booking_basedondate_cursor%NOTFOUND and v_countforchecking = 1 then
   DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'End of RECORD for the year '||in_year||'!!!');
   CLOSE pending_booking_basedondate_cursor;
   EXIT;
  elsif pending_booking_basedondate_cursor%NOTFOUND then
  DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'NO RECORD for the year '||in_year||'!!!');
  CLOSE pending_booking_basedondate_cursor;
  EXIT;
  else
    DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE('('||v_count||')'||' Customer ('||pending_booking_basedondate_record."Customer Name"||')');
    DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE('Booking Status               :'||pending_booking_basedondate_record."Journey Status");
    DBMS_OUTPUT.PUT_LINE('Booking Date                 :'||pending_booking_basedondate_record."Booking Date");
    DBMS_OUTPUT.PUT_LINE('Location From                :'||pending_booking_basedondate_record."Location From");
    DBMS_OUTPUT.PUT_LINE('Location From                :'||pending_booking_basedondate_record."Desired Location");
    DBMS_OUTPUT.PUT_LINE('Customer Age                 :'||pending_booking_basedondate_record."Customer Age");
    DBMS_OUTPUT.PUT_LINE('Customer Phone number        :'||pending_booking_basedondate_record."Customer Phone No");
    DBMS_OUTPUT.PUT_LINE('Customer Gender              :'||pending_booking_basedondate_record."Gender");
  v_count:=v_count+1;
  v_countforchecking := v_countforchecking+1;
 end if;
END Loop;


DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------'); 
DBMS_OUTPUT.PUT_LINE(rpad('.',30)||'END OF REPORT'); 
DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------'); 
END;
/

我希望我的报告是这样的(如果有记录的话):

SQL> exec booking_pending_report(3,2021)

----------------------------------------------------------------------------
.                             ON DEMAND REPORT
.     Booking that are still on "Pending" in year 2021
-----------------------------------------------------------------------------
--------------------------------------------------
(1) Customer (Hollee)
--------------------------------------------------
Booking Status               :Pending
Booking Date                 :22/MAR/2021
Location From                :Kedah
Location From                :Pahang
Customer Age                 :31 years old
Customer Phone number        :013-5941276
Customer Gender              :Female
--------------------------------------------------
(2) Customer (Casey)
--------------------------------------------------
Booking Status               :Pending
Booking Date                 :24/MAR/2021
Location From                :Perak
Location From                :Johor
Customer Age                 :21 years old
Customer Phone number        :016-3818244
Customer Gender              :Female
.                     **End OF RECORD** for the year 2021!!!
-----------------------------------------------------------------------------
.                             END OF REPORT
-----------------------------------------------------------------------------

但是,输出一直给我这个结果:

SQL> exec booking_pending_report(3,2021)

-----------------------------------------------------------------------------
.                             ON DEMAND REPORT
.     Booking that are still on "Pending" in year 2021
-----------------------------------------------------------------------------
--------------------------------------------------
(1) Customer (Hollee)
--------------------------------------------------
Booking Status               :Pending
Booking Date                 :22/MAR/2021
Location From                :Kedah
Location From                :Pahang
Customer Age                 :31 years old
Customer Phone number        :013-5941276
Customer Gender              :Female
--------------------------------------------------
(2) Customer (Casey)
--------------------------------------------------
Booking Status               :Pending
Booking Date                 :24/MAR/2021
Location From                :Perak
Location From                :Johor
Customer Age                 :21 years old
Customer Phone number        :016-3818244
Customer Gender              :Female
.                      **NO RECORD** for the year 2021!!! >> it doesn't make sense as there is a record
-----------------------------------------------------------------------------
.                             END OF REPORT
-----------------------------------------------------------------------------

我只想在以下情况下显示无记录:

SQL> exec booking_pending_report(3,2028)
-----------------------------------------------------------------------------
.                             ON DEMAND REPORT
.     Booking that are still on "Pending" in year 2028
-----------------------------------------------------------------------------
.                      **NO RECORD** for the year 2028!!!
-----------------------------------------------------------------------------
.                             END OF REPORT
-----------------------------------------------------------------------------

我怀疑循环中的 if else 语句有问题!

【问题讨论】:

  • 每次提取都会评估该语句。该语句被打印是因为还有一个 fetch (在最后一条记录之后产生“未找到”)。
  • @MarkRotteveel 但我在我的第一个 if 语句中阻止了它不是吗?我只是想知道为什么不被执行!
  • 说你有两条记录,第一次fetch找到记录,所以显示,第二次fetch找到记录,所以显示,第三次fetch没有找到记录,v_count_for_checking是2(不是 1),所以它会触发第二个 if。
  • 天哪!非常感谢你,它工作kssssss。感谢您解释流程!深表感谢

标签: sql oracle if-statement stored-procedures plsql


【解决方案1】:

这是你制作灯笼裤的地方。

if pending_booking_basedondate_cursor%NOTFOUND and v_countforchecking = 1 then

您的光标返回两条记录,因此 v_countforchecking 不等于 1,因此控制转移到 elsif 条件;这只是检查pending_booking_basedondate_cursor%NOTFOUND,这是真的。

第一个条件应该检查​​

if pending_booking_basedondate_cursor%NOTFOUND and v_countforchecking >= 1 then

【讨论】:

    【解决方案2】:

    @APC 已经回答了您的问题。但是您可以考虑另一种可能更有效的方法来创建相同的报告。

    DECLARE
        -- ALL OTHER VARIABLES
    
        v_counter INTEGER := 0;
        type t_booking_type is table of pending_booking_basedondate_cursor%rowtype;
        t_booking t_booking_type;
    
        cst_bulk_size CONSTANT INTEGER := 100;
        
    BEGIN
        -- YOUR CODE
    OPEN pending_booking_basedondate_cursor;
    
    LOOP
        FETCH pending_booking_basedondate_cursor BULK COLLECT INTO t_booking LIMIT cst_bulk_size;
        
        FOR i IN 1..t_booking.COUNT() LOOP
    
            v_counter := v_counter + 1;
            
            DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');
            DBMS_OUTPUT.PUT_LINE('('||v_counter||')'||' Customer ('||t_booking(i)."Customer Name"||')');
            DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');
            DBMS_OUTPUT.PUT_LINE('Booking Status               :'||t_booking(i)."Journey Status");
            DBMS_OUTPUT.PUT_LINE('Booking Date                 :'||t_booking(i)."Booking Date");
            DBMS_OUTPUT.PUT_LINE('Location From                :'||t_booking(i)."Location From");
            DBMS_OUTPUT.PUT_LINE('Location From                :'||t_booking(i)."Desired Location");
            DBMS_OUTPUT.PUT_LINE('Customer Age                 :'||t_booking(i)."Customer Age");
            DBMS_OUTPUT.PUT_LINE('Customer Phone number        :'||t_booking(i)."Customer Phone No");
            DBMS_OUTPUT.PUT_LINE('Customer Gender              :'||t_booking(i)."Gender");
        END LOOP;   
        EXIT WHEN t_employees.COUNT() <> cst_bulk_size;
      
    END LOOP;
    
    IF t_employees.COUNT() <> 0 THEN
        DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'End of RECORD for the year '||in_year||'!!!');
    ELSE
        DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'NO RECORD for the year '||in_year||'!!!');
    END IF;
    
    CLOSE pending_booking_basedondate_cursor;
    
    DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------'); 
    DBMS_OUTPUT.PUT_LINE(rpad('.',30)||'END OF REPORT'); 
    DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------------------------------'); 
    
    END;
    /
    
    
    

    【讨论】:

    • 非常感谢您的建议,我稍后试一试并在此处更新我的任何问题要问您! :D
    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多