如何根据记录的存在或不存在显示消息?

How to show message depending on presence or absence of records?

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

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 语句有问题!

这里是你大放异彩的地方。

if pending_booking_basedondate_cursor%NOTFOUND and v_countforchecking = 1 then

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

第一个条件应该检查​​

if pending_booking_basedondate_cursor%NOTFOUND and v_countforchecking >= 1 then

@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;
/