【问题标题】:sql plus stops responding after calling a stored proceduresql plus 调用存储过程后停止响应
【发布时间】:2020-06-26 01:12:43
【问题描述】:
create or replace procedure pizza_qty(order_num IN order_list.order_no%type)  is
    cursor c1 is 
       select order_no,qty from order_list; 
    temp1 order_list.order_no%type;
    pizzacount order_list.qty%type;
    temp2 order_list.qty%type;
begin
    open c1;
    loop
        fetch c1 into temp1,temp2;
        if order_num=temp1 then
            pizzacount := pizzacount+1;
            --exit;
        elsif c1%notfound and pizzacount=0 then
            dbms_output.put_line('the order '||order_num||' is invalid..');
            exit;
        end if;
    end loop;
    dbms_output.put_line('the order '||order_num||' has '||pizzacount||' pizza(s) to be delivered');
    close c1;
end;
/

【问题讨论】:

    标签: oracle plsql dbms-output


    【解决方案1】:

    你写的代码……嗯,很奇怪。它假定该表中的所有订单都应该只包含比萨饼。虽然,这不是你的问题的原因。

    声明变量pizzacount,将其设置为null。任何+ null 仍然是null,所以它永远不会设置为其他任何东西,所以你的循环永远不会退出。

    此外,您不需要光标;一个简单的select 就足够了。如果它返回该订单号的东西,那么那些必须是比萨饼。否则,它根本不会返回任何东西,因此订单号无效。

    类似这样:首先测试用例:

    SQL> create table order_list (order_no number, qty number);
    
    Table created.
    
    SQL> insert into order_list (order_no, qty) values (1, 5);
    
    1 row created.
    
    SQL>
    

    程序:

    SQL> create or replace procedure pizza_qty (par_order_num in order_list.order_no%type)
      2  is
      3    l_qty order_list.qty%type;
      4  begin
      5    select qty
      6      into l_qty
      7      from order_list
      8      where order_no = par_order_num;
      9
     10    dbms_output.put_line('Order ' || par_order_num || ' has ' || l_qty ||
     11                         ' pizza(s) to be delivered');
     12  exception
     13    when no_data_found then
     14      dbms_output.put_line('Order ' || par_order_num || ' is invalid');
     15  end;
     16  /
    
    Procedure created.
    
    SQL>
    

    测试:

    SQL> set serveroutput on;
    SQL> exec pizza_qty(1);
    Order 1 has 5 pizza(s) to be delivered
    
    PL/SQL procedure successfully completed.
    
    SQL> exec pizza_qty(2);
    Order 2 is invalid
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    【讨论】:

    • 感谢您的宝贵时间,这真的很有帮助
    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多