Assignment 8)
Show simple Implementation of cursor
-- Assume we have a Student table
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100),
Age INT,
Course VARCHAR(50)
);
-- Insert sample data
INSERT INTO Student VALUES (1, 'Naveen', 22, 'Computer
Science');
INSERT INTO Student VALUES (2, 'Ravi', 19, 'Mathematics');
INSERT INTO Student VALUES (3, 'Priya', 21, 'Physics');
-- Cursor example
DECLARE @StudentName VARCHAR(100);
-- Define the cursor
DECLARE student_cursor CURSOR FOR
SELECT StudentName FROM Student;
-- Open the cursor
OPEN student_cursor;
-- Fetch the first row
FETCH NEXT FROM student_cursor INTO @StudentName;
-- Loop through all rows
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Student Name: ' + @StudentName;
FETCH NEXT FROM student_cursor INTO @StudentName;
END;
-- Close and deallocate
CLOSE student_cursor;
DEALLOCATE student_cursor;