/* Printing variables -- this has to come first */

VARIABLE gpapr NUMBER;

DECLARE
 	gpa NUMBER := 0;
	ncourses NUMBER := 0;
	grd  Transcripts.Grade%Type;    /* CHAR(1) */

	CURSOR allRecs IS 
		SELECT T.Grade 
		FROM Transcripts T 
		WHERE T.studid = 111111111;
BEGIN

	OPEN allRecs;

	LOOP 
	 /* Retrieve each row of the result of the above query into PL/SQL variables: */

	FETCH allRecs INTO grd; 

	/* If there are no more rows to fetch, exit the loop: */ 

	EXIT WHEN allRecs%NOTFOUND; 

	IF (grd = 'A') THEN gpa := gpa + 4; ncourses := ncourses + 1; END IF;
	IF (grd = 'B') THEN gpa := gpa + 3; ncourses := ncourses + 1; END IF;
	IF (grd = 'C') THEN gpa := gpa + 2; ncourses := ncourses + 1; END IF;
	IF (grd = 'D') THEN gpa := gpa + 1; ncourses := ncourses + 1; END IF;
	IF (grd = 'F') THEN gpa := gpa + 0; ncourses := ncourses + 1; END IF;

        END LOOP;
        
	CLOSE allRecs; 

	IF (ncourses>0) THEN gpa := gpa / ncourses; END IF;

	:gpapr := gpa;

END;
.
RUN;
