sql加号在调用存储过程后停止响应

问题描述 投票:0回答:1
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个回答
0
投票

您编写的代码很……很奇怪。假定该表中的所有订单都只能包含披萨。不过,这不是您遇到问题的原因。

声明变量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>
© www.soinside.com 2019 - 2024. All rights reserved.