ORA-01001:在Oracle SQL Developer中使用函数时无效的游标?

时间:2017-10-22 00:25:09

标签: oracle function oracle-sqldeveloper

我创建了一个以Customer_id作为输入的函数,并返回一个代表该客户订单总成本的数字。我必须将表order_details中的Order_Qty与表项中的Unit_Price相乘,以获得每个订单的总成本。

这是我的代码:

CREATE OR REPLACE FUNCTION FIND_TOTAL
(CUSTOMER_ID_IN IN VARCHAR2)
RETURN NUMBER IS
ORDER_QTY_IN NUMBER;
UNIT_PRICE_IN NUMBER;
TOTAL_COST_OUT NUMBER;
CURSOR C1 IS
SELECT od.ORDER_QTY
FROM ORDER_DETAILS od JOIN ORDERS o 
ON od.ORDER_ID = o.ORDER_ID
WHERE CUSTOMER_ID = CUSTOMER_ID_IN;
CURSOR C2 IS
SELECT i.UNIT_PRICE
FROM ITEMS i JOIN ORDER_DETAILS od
ON i.ITEM_ID = od.ITEM_ID
JOIN ORDERS o 
ON o.ORDER_ID = od.ORDER_ID
WHERE CUSTOMER_ID = CUSTOMER_ID_IN;
BEGIN
    OPEN C1;
        FETCH C1 INTO UNIT_PRICE_IN;
        IF C1%FOUND AND C2%FOUND THEN
        TOTAL_COST_OUT := ORDER_QTY_IN*UNIT_PRICE_IN;
    END IF;
    CLOSE C1;
    CLOSE C2;
    RETURN TOTAL_COST_OUT;
END;
/

现在我必须使用该函数编写一个select语句,用于选择所有客户以及他们所做订单的总成本。

SELECT CUSTOMER_ID, FIND_TOTAL(CUSTOMER_ID) AS TOTAL_COST
FROM CUSTOMERS;

但是当我这样做时,我收到一个错误:

ORA-01001: invalid cursor
ORA-06512: at "TUG81959.FIND_TOTAL", line 22
01001. 00000 -  "invalid cursor"
*Cause:    
*Action:

这是我的表格列表,以防有用: enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你可以在引用C2%FOUND之前打开并固定C2光标。

让我知道。

此致