⬅ Previous Topic
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINTNext Topic ⮕
SQL Error Handling – BEGIN...HANDLER, SIGNAL⬅ Previous Topic
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINTNext Topic ⮕
SQL Error Handling – BEGIN...HANDLER, SIGNALSometimes in SQL, we need to process each row one by one — not all at once like regular queries. That's where cursors come into play. Think of a cursor like a for-loop that allows you to work with one row at a time. In school systems, this could be useful for assigning ranks, sending notifications, or logging updates for each student individually.
A cursor is a database object used to retrieve and process one row at a time from a query result set. It’s most useful when row-wise operations are needed — operations that aren’t possible with pure SQL set logic.
DECLARE
– Define the cursor with a SELECT queryOPEN
– Initialize the cursorFETCH
– Retrieve the next rowCLOSE
– Release the cursorCREATE TABLE students (
roll_no INT,
name VARCHAR(50),
marks INT
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', 85),
(2, 'Diya Iyer', 92),
(3, 'Sneha Patil', 78),
(4, 'Mehul Agarwal', 90);
DELIMITER $$
CREATE PROCEDURE print_students()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE s_name VARCHAR(50);
DECLARE student_cursor CURSOR FOR
SELECT name FROM students;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN student_cursor;
read_loop: LOOP
FETCH student_cursor INTO s_name;
IF done THEN
LEAVE read_loop;
END IF;
SELECT CONCAT('Student: ', s_name);
END LOOP;
CLOSE student_cursor;
END$$
DELIMITER ;
CALL print_students();
Student: Aarav Sharma
Student: Diya Iyer
Student: Sneha Patil
Student: Mehul Agarwal
CREATE TABLE student_grades (
roll_no INT,
grade VARCHAR(2)
);
DELIMITER $$
CREATE PROCEDURE assign_grades()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE s_roll INT;
DECLARE s_marks INT;
DECLARE grade VARCHAR(2);
DECLARE cur CURSOR FOR
SELECT roll_no, marks FROM students;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
loop_through: LOOP
FETCH cur INTO s_roll, s_marks;
IF done THEN
LEAVE loop_through;
END IF;
-- Simple grading logic
IF s_marks >= 90 THEN
SET grade = 'A+';
ELSEIF s_marks >= 75 THEN
SET grade = 'A';
ELSE
SET grade = 'B';
END IF;
INSERT INTO student_grades VALUES (s_roll, grade);
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
CALL assign_grades();
SELECT * FROM student_grades;
roll_no | grade
--------+-------
1 | A
2 | A+
3 | B
4 | A+
Cursors
give SQL the ability to step through each row of a result set and perform actions one at a time. While powerful, they should be used wisely. When regular queries fall short, cursors let you take control — row by row, decision by decision.
Next, we’ll look into SQL Optimization Techniques to make your queries more efficient and your database faster.
DECLARE student_cursor CURSOR FOR
SELECT name FROM students;
OPEN student_cursor;
FETCH NEXT FROM student_cursor;
CLOSE student_cursor;
What does this code do?⬅ Previous Topic
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINTNext Topic ⮕
SQL Error Handling – BEGIN...HANDLER, SIGNALYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.