--分组后可能无法再 cust_row 直接引用 sum(s.totalmoney) 这时候,直接将 sum(s.totalmoney) 用as 重新命名即可调用.
CREATE OR REPLACE PROCEDURE CUST_DEMO AS
CUST_NUM NUMBER;
AVGMONEY NUMBER;
CURSOR CUST_CUR IS
SELECT C.NAME, SUM(S.TOTALMONEY) AS T
FROM CUSTOM C, SALERECORD S
WHERE C.ID = S.CUSTOMERID
GROUP BY C.NAME;
BEGIN
SELECT COUNT(*)
INTO CUST_NUM
FROM (SELECT S.CUSTOMERID FROM SALERECORD S GROUP BY S.CUSTOMERID);
SELECT SUM(TOTALMONEY) / CUST_NUM INTO AVGMONEY FROM SALERECORD;
DBMS_OUTPUT.PUT_LINE('平均值为:' || AVGMONEY);
FOR CUST_ROW IN CUST_CUR LOOP
DBMS_OUTPUT.PUT_LINE(CUST_ROW.NAME || ' ' || CUST_ROW.T);
END LOOP;
END CUST_DEMO;