Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Nested cursor loop problem

491384Feb 23 2006 — edited Feb 23 2006
Hey guys, this is an offspring of my last thread, but I felt the need to start a new one to move beyond all the clutter and focus on this problem in particular. Thanks for the help on that BTW!

Ok here goes. I have a cursor that is calling a DISTINCT field and another cursor that is gathering all the rows that correspond to that distinct field.

The cursors and all the rest of the code compile fine. But when I go to use the second cursor in the nested loop I get all sort of compile errors. The first of which is "134/21 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
. into bulk"

Here is my entire code with the parts that are having the problems commented. Can anyone spot any basic logic errors or syntax errors I am unaware of?

Thanks alot!
CREATE OR REPLACE PROCEDURE get_Student_GPA(p_StudentID IN NUMBER) AS

	/*VARIABLES*/
	v_Terms VARCHAR2(6);
	v_Courses VARCHAR2(6);
	v_Scores NUMBER;
	v_Grade CHAR;
	v_GPA NUMBER;
	v_ScoresTotal NUMBER :=0;
	v_StudentID NUMBER;
	
	
	/*CURSORS*/
	CURSOR c_GetTerms IS
		SELECT Term 
		FROM course_grades 
		WHERE STUDENT_ID = p_StudentID;

	CURSOR c_GetCourseAndGrade IS
		SELECT Course_ID, Score FROM course_grades 
		WHERE STUDENT_ID = p_StudentID;

	/* THESE NEXT TWO CURSORS ARE NESTED */
	CURSOR c_GPAPerTerm IS
		SELECT DISTINCT Term
		FROM course_grades 
		WHERE STUDENT_ID = p_StudentID;


	CURSOR c_GetScores (p_Term VARCHAR2) IS		
			SELECT Score 
			FROM course_grades
			WHERE Term = p_Term AND Student_ID = p_StudentID;


	
	/*FUNCTIONS*/
	FUNCTION convert_grade(p_GradeNumber IN NUMBER) 
	
		RETURN CHAR IS
	BEGIN
	
		/* GET NUMERIC GRADE AND CONVERT TO LETTER */

		CASE 
			WHEN p_GradeNumber < 60 THEN RETURN 'F';
			WHEN (p_GradeNumber > 59  AND p_GradeNumber < 70) THEN  RETURN 'D';
			WHEN (p_GradeNumber > 69  AND p_GradeNumber < 80) THEN  RETURN 'C';
			WHEN (p_GradeNumber > 79  AND p_GradeNumber < 90) THEN  RETURN 'B';
			WHEN (p_GradeNumber > 89  AND p_GradeNumber < 101) THEN RETURN 'A';
	
		ELSE    RETURN 'Z';
	
		END CASE;

	END convert_grade;


	/**********************************************************/
	FUNCTION calculate_gpa(p_TotalHourPoints IN NUMBER, p_TotalHours IN NUMBER)

		RETURN NUMBER IS

		/*CREATE VARIABLE TO HOLD GPA*/
		v_GPA NUMBER;

	BEGIN


		/*CALCULATE AND OUTPUT GPA*/
		v_GPA := p_TotalHourPoints/p_TotalHours;
		RETURN v_GPA;

	END calculate_gpa;


	/**********************************************************/
	FUNCTION calculate_point (p_Grade IN CHAR) 

		RETURN NUMBER IS

	BEGIN

		/* GET LETTER GRADE AND CONVERT TO NUMBER */

		CASE 
			WHEN p_Grade = 'A' THEN RETURN 4;
			WHEN p_Grade = 'B' THEN RETURN 3;
			WHEN p_Grade = 'C' THEN RETURN 2;
			WHEN p_Grade = 'D' THEN RETURN 1;
			WHEN p_Grade = 'F' THEN RETURN 0;
	
		ELSE    RETURN 0;
	
		END CASE;

	END calculate_point ;


	
/****BEGIN MAIN BLOCK********/

BEGIN
	
	DBMS_OUTPUT.PUT_LINE('**********TERMS**********');
	OPEN c_GetTerms;
	LOOP
	FETCH c_GetTerms INTO v_Terms;
	DBMS_OUTPUT.PUT_LINE('Term: ' || v_Terms);
	EXIT WHEN c_GetTerms%NOTFOUND;
	END LOOP;
	CLOSE c_GetTerms;

	
	DBMS_OUTPUT.PUT_LINE('**********COURSES AND GRADES**********');
	OPEN c_GetCourseAndGrade;
	LOOP
	FETCH c_GetCourseAndGrade INTO v_Courses, v_Scores;
        v_Grade := convert_grade(v_Scores);
	DBMS_OUTPUT.PUT_LINE('Course: ' || v_Courses || '   Grade: ' || v_Grade);
	EXIT WHEN c_GetCourseAndGrade%NOTFOUND;
	END LOOP;
	CLOSE c_GetCourseAndGrade;


	DBMS_OUTPUT.PUT_LINE('**********GPA PER TERM**********');
	OPEN c_GPAPerTerm;
	LOOP

	FETCH c_GPAPerTerm INTO v_Terms;
			
			/*ERROR HERE USING THIS CURSOR*/
		        OPEN c_GetScores;
			LOOP
			FETCH c_GetScores(v_Terms) INTO v_Scores; 
			v_ScoresTotal := v_ScoresTotal + v_Scores;
			EXIT WHEN c_GetScores%NOTFOUND;
			END LOOP;
			CLOSE c_GetScores;/*END ERROR PART*/
		
        		v_GPA := calculate_gpa(v_ScoresTotal, 3);
			v_ScoresTotal :=0;
			DBMS_OUTPUT.PUT_LINE('Term: ' || v_Terms || '   GPA: ' || v_GPA);
		
	EXIT WHEN c_GPAPerTerm%NOTFOUND;
	END LOOP;
	CLOSE c_GPAPerTerm;


END get_Student_GPA;
/
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 23 2006
Added on Feb 23 2006
2 comments
966 views