pl / sql查询无法运行

时间:2017-05-22 02:39:29

标签: sql plsql

我只是学习PL / SQL,我无法理解为什么这个循环中的语句独立运行,但PL / SQL过程执行时根本没有显示任何结果。有人能给我任何建议吗?谢谢。

set serveroutput on size 2000

DECLARE
    CURSOR money_cur IS 
        SELECT SUM(etotal) AS Total_income
        FROM a_enrolment
        GROUP BY etotal;

    etotal a_enrolment%ROWTYPE;
    total_income number;

BEGIN
    DBMS_OUTPUT.PUT_LINE( chr(10) ); 

    DBMS_OUTPUT.PUT_LINE( 'Displaying total income generated' || chr(10) );

    FOR etotal IN money_cur LOOP
        DBMS_OUTPUT.PUT_LINE('Total income is ' || Total_income);
    END LOOP;
END;
/

2 个答案:

答案 0 :(得分:0)

DECLARE
    CURSOR money_cur IS 
        SELECT SUM(etotal) AS Total_income
        FROM a_enrolment
        GROUP BY etotal;

    --etotal a_enrolment%ROWTYPE;
    --total_income number;

BEGIN
    DBMS_OUTPUT.PUT_LINE( chr(10) ); 

    DBMS_OUTPUT.PUT_LINE( 'Displaying total income generated' || chr(10) );
FOR etotal IN money_cur LOOP
        DBMS_OUTPUT.PUT_LINE('Total income is ' || etotal.Total_income);
    END LOOP;
END;
/

答案 1 :(得分:0)

谢谢大家的帮助:)我真的很感激。以下是我发现如果您遇到类似问题我可以解决问题的另一种方法。

DECLARE
    CURSOR student_cur IS
    SELECT sid, etotal, edate
    FROM a_enrolment
    ORDER BY sID;

    grand_total number := 0;

total a_enrolment%ROWTYPE;

BEGIN -- this whole block is the proceedure
DBMS_OUTPUT.PUT_LINE( chr(10) );
    DBMS_OUTPUT.PUT_LINE( 'Displaying student details' || chr(10) );
    DBMS_OUTPUT.PUT_LINE(RPAD('ID',15) || RPAD('TOTAL',15) || 'DATE');
FOR total IN student_cur LOOP
DBMS_OUTPUT.PUT_LINE(RPAD(total.sID, 15) || RPAD(total.etotal, 15) || 
total.edate);
grand_total := grand_total + total.etotal; -- this is the function
END LOOP;

DBMS_OUTPUT.PUT_LINE('The grand total is $' || grand_total); 

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data has been returned');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too much data has been returned');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Oracle error code: ' || SQLCODE || ' - Message: ' ||
SQLERRM);

END;
/