Demonstration of Cursor in Oracle to Print Average Marks of Students

Demonstration of Cursor in Oracle to Print Average Marks of Students 


DECLARE

   CURSOR Marks_cursor IS

      SELECT marks

      FROM studentt;


   total_marks NUMBER := 0;

   total_students NUMBER := 0;

   average_marks NUMBER(4,2);


BEGIN

   OPEN Marks_cursor;


   LOOP

      FETCH Marks_cursor INTO total_students;


      EXIT WHEN Marks_cursor%NOTFOUND;


      total_marks := total_marks + total_students;

      total_students := total_students + 1;

   END LOOP;

  CLOSE Marks_cursor;


   IF total_students > 0 THEN

      average_marks := total_marks / total_students;

      DBMS_OUTPUT.PUT_LINE('Average marks of all students: ' || average_marks);

   ELSE

      DBMS_OUTPUT.PUT_LINE('No students found.');

   END IF;


END;

/


Result:

SQL> @C:\Users\Admin\Desktop\Oracle\cursor3.sql
Average marks of all students: 9.6

PL/SQL procedure successfully completed.

टिप्पणी पोस्ट करा

0 टिप्पण्या