Cursor in Oracle to Calculate Total Marks of Students

Cursor in Oracle to Calculate Total Marks of Students 

 DECLARE

   CURSOR Marks_cursor IS

      SELECT student_id, student_name, internal_marks, external_marks

      FROM studentsss;


   student_id studentsss.student_id%TYPE;

   student_name studentsss.student_name%TYPE;

   internal_marks studentsss.internal_marks%TYPE;

   external_marks studentsss.external_marks%TYPE;


   total_marks NUMBER := 0;

   total_students NUMBER := 0;


BEGIN

   OPEN Marks_cursor;


   LOOP

      FETCH Marks_cursor INTO student_id, student_name, internal_marks, external_marks;


      EXIT WHEN Marks_cursor%NOTFOUND;


      total_marks := internal_marks + external_marks;

      total_students := total_students + 1;


      DBMS_OUTPUT.PUT_LINE('Studentid:' ||student_id || ', Student Name: ' || student_name || ', Total Marks: ' || total_marks);

   END LOOP;


   CLOSE Marks_cursor;


   IF total_students = 0 THEN

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

   END IF;


END;

/


Result:

SQL> @C:\Users\Admin\Desktop\Oracle\Cursor4.sql

Studentid:1, Student Name: Samiksha, Total Marks: 94

Studentid:2, Student Name: Rahul, Total Marks: 95

Studentid:3, Student Name: Ruchira, Total Marks: 96

Studentid:4, Student Name: Sagar, Total Marks: 89

Studentid:5, Student Name: Rudra, Total Marks: 94

Studentid:6, Student Name: Rucha, Total Marks: 78

Studentid:7, Student Name: Rucha, Total Marks: 83

Studentid:8, Student Name: Rucha, Total Marks: 95

Studentid:9, Student Name: Rupesh, Total Marks: 87

Studentid:10, Student Name: Rohan, Total Marks: 94


PL/SQL procedure successfully completed.


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

3 टिप्पण्या

कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏