SQL过程 - 没有打印出

时间:2017-05-31 11:11:31

标签: oracle exception plsql

当我尝试运行此代码时,它没有显示任何输出,我不知道为什么。

此过程接收客户ID并显示所有订单     由指定客户制作。它使用游标来存储细节     订单然后打印详细信息。

SET SERVEROUTPUT ON;
    create or replace PROCEDURE procedure_CustOrderHistory (cust_id IN number) AS
    CURSOR c_CustOrderHistory  IS

    Select o.order_id, o.order_totalCost, o.order_date, cu.customer_firstName, 
    cu.customer_lastName, s.staff_firstName, s.staff_lastName, os.order_status
    FROM orders o, customer cu, staff s, order_status os
    where o.customer_id=cu.customer_id AND s.staff_id = o.staff_id AND
    os.order_statusID = o.order_statusID and cu.customer_id = cust_id ;

BEGIN
  FOR r_order IN c_CustOrderHistory
  LOOP 
   dbms_output.put_line('Order ID: '||r_order.order_id || ', Order Total Cost: ' || r_order.order_totalCost || 'BHD, Order Date: ' || r_order.order_date || ', Customer Name: ' || r_order.customer_firstName || ' ' || r_order.customer_lastName|| ', Customer Name: ' || r_order.customer_firstName || ' ' || r_order.customer_lastName || ', Order Status: '|| r_order.order_status);              
  END LOOP;
END;

此匿名块测试上一过程。它询问用户     对于ID,然后通过传入客户ID来调用该过程。

SET SERVEROUTPUT ON;
DECLARE
  v_custid number := &customerid;
BEGIN
  procedure_CustOrderHistory(v_custid);
  EXCEPTION
  WHEN NO_DATA_FOUND THEN
    dbms_output.put_line('Customer ID not found.');
  WHEN OTHERS THEN
    dbms_output.put_line('Error! ' || SQLERRM);
END;

1 个答案:

答案 0 :(得分:0)

游标循环不会抛出NO_DATA_FOUND。因此,如果您的查询没有返回任何行,则光标将循环零次并优雅地退出。

因此,最有可能发生的事情是,您在提示符处传递的ID没有customer条记录,或者游标查询中存在其他逻辑问题(例如,客户没有订单)。

你可以通过添加这样的计数器来测试我的理论:

create or replace PROCEDURE procedure_CustOrderHistory (cust_id IN number) AS
    CURSOR c_CustOrderHistory  IS

    Select o.order_id, o.order_totalCost, o.order_date, cu.customer_firstName, 
    cu.customer_lastName, s.staff_firstName, s.staff_lastName, os.order_status
    FROM orders o, customer cu, staff s, order_status os
    where o.customer_id=cu.customer_id AND s.staff_id = o.staff_id AND
    os.order_statusID = o.order_statusID and cu.customer_id = cust_id ;
    rec_ctr pls_integer := 0;
BEGIN
  FOR r_order IN c_CustOrderHistory
  LOOP 
    rec_ctr := rec_ctr +1;
   dbms_output.put_line('#'|| rec_ctr ||' '|| 
           'Order ID: '||r_order.order_id || ', Order Total Cost: ' || r_order.order_totalCost || 'BHD, Order Date: ' || r_order.order_date || ', Customer Name: ' || r_order.customer_firstName || ' ' || r_order.customer_lastName|| ', Customer Name: ' || r_order.customer_firstName || ' ' || r_order.customer_lastName || ', Order Status: '|| r_order.order_status);              
  END LOOP;
  if rec_ctr = 0 then
     raise no_data_found;
  end if;
END;

如果循环没有处理任何行,则显式引发异常。

  

"。但是,即使我输入有效的CustomerID,它也不会显示它。你有什么想法吗?"

您的查询加入了四个表。因此,您需要通过查询的逻辑来确保:

  1. 正确指定了连接条件
  2. 表格中有匹配的数据
  3. 例如,您输入的客户是否有订单?如果是,他们的订单是否具有有效的订单状态。订单员工ID是否有效?