You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle存储过程如何根据查询记录存在与否输出对应报表提示信息

Oracle存储过程报表输出逻辑错误修复

问题原因

核心错误出在循环末尾的分支判断逻辑:
你定义的v_countforchecking是已输出的记录计数,每输出1条记录就+1,现有分支要求v_countforchecking = 1才触发「有记录的结束提示」,但只要记录数≥2,游标遍历完成触发%NOTFOUND时,v_countforchecking的数值就会大于1,就会匹配到下一个%NOTFOUND的分支,错误输出无记录提示。

修复方案

把循环内的IF判断条件修改为判断计数器是否大于0即可,代码如下:

Loop
 fetch pending_booking_basedondate_cursor into pending_booking_basedondate_record;

  if pending_booking_basedondate_cursor%NOTFOUND then
    -- 计数器大于0说明有输出过记录,打印正常结束提示
    if v_countforchecking > 0 then
      DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'End of RECORD for the year '||in_year||'!!!');
    else
      -- 计数器为0说明没有匹配记录
      DBMS_OUTPUT.PUT_LINE(rpad('.',23)||'NO RECORD for the year '||in_year||'!!!');
    end if;
    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('Desired Location             :'||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;

额外优化建议

  • 现有查询的日期区间写死了结束日期为当月30号,会导致1/3/5/7/8/10/12月的31号数据查询不到,2月也会出现日期不合法的问题,可以改成last_day(to_date('01-'||in_month||'-'||in_year,'dd-mm-yyyy'))作为结束日期,自动适配每个月的最后一天。
  • 原有输出字段里连续打印了两次「Location From」,第二个应该是「Desired Location」,上述修正代码里已经做了调整,避免输出歧义。

内容的提问来源于stack exchange,提问作者Joshua Tabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 11:27:03