如何将光标传输到程序?

时间:2016-12-08 00:33:05

标签: sql oracle plsql

DECLARE
    CURSOR cursor1 IS SELECT STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE
    FROM STUDENTINFO
    WHERE STUDENTNAME = '&student'
    GROUP BY (STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE)
    ORDER BY COURSEID;
    S_NAME STUDENTINFO.STUDENTNAME%TYPE;
    S_COURSEID STUDENTINFO.COURSEID%TYPE;
    S_COURSEDESCRIPTION STUDENTINFO.COURSEDESCRIPTION%TYPE;
    S_COURSECREDITS STUDENTINFO.COURSECREDITS%TYPE;
    S_GRADE STUDENTINFO.GRADE%TYPE;
    S_TOTALCREDITS STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_A STUDENTINFO.COURSECREDITS%TYPE := 4;
    S_B STUDENTINFO.COURSECREDITS%TYPE := 3;
    S_C STUDENTINFO.COURSECREDITS%TYPE := 2;
    S_D STUDENTINFO.COURSECREDITS%TYPE := 1;
    S_F STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_GPABEFOREDIV STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_CREDITSTOGPA STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_GPA NUMBER(4,2);
BEGIN
    OPEN CURSOR1;
    LOOP 
    FETCH CURSOR1 INTO S_NAME, S_COURSEID, S_COURSEDESCRIPTION, S_COURSECREDITS, S_GRADE;
    EXIT WHEN cursor1%NOTFOUND;
        IF (S_GRADE = 'A') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_A);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'B') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_B);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'C') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_C);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'D') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_D);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSE
        S_CREDITSTOGPA := (S_COURSECREDITS * S_F);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    END IF;
    DBMS_OUTPUT.PUT_LINE(CHR(10));
    DBMS_OUTPUT.PUT_LINE(S_COURSEID ||' '|| S_COURSEDESCRIPTION ||' '|| S_COURSECREDITS ||' '|| S_GRADE);
    S_TOTALCREDITS := S_TOTALCREDITS + S_COURSECREDITS;
    S_GPA := (S_GPABEFOREDIV / S_TOTALCREDITS);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total Credits: ' || S_TOTALCREDITS);
    DBMS_OUTPUT.PUT_LINE('Overall GPA: ' || S_GPA);
    CLOSE CURSOR1;
END;
/

这是我在收到输入后输出学生GPA的光标。我需要将其转移到参数(学生姓名)中的1和1(gpa)中的过程。我完全迷失在哪里开始。我假设我必须在某个地方使用循环才能累计每个课程的学分,对吗?

2 个答案:

答案 0 :(得分:1)

我假设您要将上述代码编写为将student_name作为输入并将gpa作为输出生成的过程。这里:

CREATE OR REPLACE PROCEDURE GPA_PR(p_student_name in varchar2, p_gpa out number) is
    CURSOR cursor1 IS SELECT STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE
    FROM STUDENTINFO
    WHERE STUDENTNAME = p_student_name
    GROUP BY (STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE)
    ORDER BY COURSEID;
    S_NAME STUDENTINFO.STUDENTNAME%TYPE;
    S_COURSEID STUDENTINFO.COURSEID%TYPE;
    S_COURSEDESCRIPTION STUDENTINFO.COURSEDESCRIPTION%TYPE;
    S_COURSECREDITS STUDENTINFO.COURSECREDITS%TYPE;
    S_GRADE STUDENTINFO.GRADE%TYPE;
    S_TOTALCREDITS STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_A STUDENTINFO.COURSECREDITS%TYPE := 4;
    S_B STUDENTINFO.COURSECREDITS%TYPE := 3;
    S_C STUDENTINFO.COURSECREDITS%TYPE := 2;
    S_D STUDENTINFO.COURSECREDITS%TYPE := 1;
    S_F STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_GPABEFOREDIV STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_CREDITSTOGPA STUDENTINFO.COURSECREDITS%TYPE := 0;
    S_GPA NUMBER(4,2);
BEGIN
    OPEN CURSOR1;
    LOOP 
    FETCH CURSOR1 INTO S_NAME, S_COURSEID, S_COURSEDESCRIPTION, S_COURSECREDITS, S_GRADE;
    EXIT WHEN cursor1%NOTFOUND;
    IF (S_GRADE = 'A') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_A);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'B') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_B);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'C') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_C);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSIF (S_GRADE = 'D') THEN
        S_CREDITSTOGPA := (S_COURSECREDITS * S_D);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    ELSE
        S_CREDITSTOGPA := (S_COURSECREDITS * S_F);
        S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA;
    END IF;
    DBMS_OUTPUT.PUT_LINE(CHR(10));
    DBMS_OUTPUT.PUT_LINE(S_COURSEID ||' '|| S_COURSEDESCRIPTION ||' '|| S_COURSECREDITS ||' '|| S_GRADE);
    S_TOTALCREDITS := S_TOTALCREDITS + S_COURSECREDITS;
    S_GPA := (S_GPABEFOREDIV / S_TOTALCREDITS);
    END LOOP;
    CLOSE CURSOR1;
    p_gpa := S_GPA;
END;
/

答案 1 :(得分:0)

您可以在程序中使用out参数返回光标。