精确提取返回超过请求的行数

时间:2013-11-12 18:40:42

标签: oracle plsql

我正在尝试创建一个程序来传递客户编号,并在过去一年内返回购买数量和购买总价值。如果没有购买,则为数字和总数返回零,并返回销售人员在过去一年中与该客户的联系人数。

我称之为:

DECLARE 
      a_Var NUMBER;
      b_Var NUMBER;
      C_Var NUMBER;
      D_Var NUMBER;    
BEGIN
      three_pr(001116,a_Var, b_Var);

    IF a_Var > 0 THEN
        DBMS_OUTPUT.PUT_LINE('the number of purchases :' || a_Var);
        DBMS_OUTPUT.PUT_LINE('the total value of purchase :' || b_Var);

    ELSE

        SELECT ContactID,Count(contactID)
        INTO
             C_Var,D_Var
        FROM    DD_Contacts
        WHERE  DateofContact  between to_date ('2012/01/01', 'yyyy/mm/dd')
               AND to_date ('2012/12/31', 'yyyy/mm/dd')
      Group By ContactID;

       DBMS_OUTPUT.PUT_LINE (C_Var||D_Var);

    END IF;

END;
/

使用上面的代码时,我收到错误:

  

ORA-01422:精确提取返回超过请求的行数ORA-06512:第16行

这是程序:

CREATE or REPLACE PROCEDURE three_pr

(par_CustomerID IN NUMBER, par_sumpurchase OUT Number,par_totalvalue OUT Number)
IS
BEGIN

        SELECT 
            COUNT(O.OrderID),SUM(Price*Quantity)
        INTO par_sumpurchase,par_totalvalue
            FROM DD_Orders O JOIN DD_OrderLine OL ON O.OrderID = OL.OrderID
            WHERE  DatePurchase  between to_date ('2012/01/01', 'yyyy/mm/dd')
            AND to_date ('2012/12/31', 'yyyy/mm/dd')
            AND CustomerID = par_CustomerID;

END;
/

1 个答案:

答案 0 :(得分:0)

当查询未返回结果时,过程three_pr无法正确处理NO_DATA_FOUND异常。

    CREATE or REPLACE PROCEDURE three_pr

    (par_CustomerID IN NUMBER, par_sumpurchase OUT Number,par_totalvalue OUT Number)
    IS
    BEGIN
        BEGIN
            SELECT 
                COUNT(O.OrderID),SUM(Price*Quantity)
            INTO par_sumpurchase,par_totalvalue
                FROM DD_Orders O JOIN DD_OrderLine OL ON O.OrderID = OL.OrderID
                WHERE  DatePurchase  between to_date ('2012/01/01', 'yyyy/mm/dd')
                AND to_date ('2012/12/31', 'yyyy/mm/dd')
                AND CustomerID = par_CustomerID;
        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                par_sumpurchase := 0;
                par_totalvalue := 0;
        END;

    END;
    /