0% found this document useful (0 votes)
12 views3 pages

Simple SQL Cursor Implementation Guide

The document provides a simple implementation of a cursor in SQL using a Student table. It includes the creation of the table, insertion of sample data, and the definition and usage of a cursor to fetch and print student names. Finally, it demonstrates how to close and deallocate the cursor after use.

Uploaded by

sighking789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Simple SQL Cursor Implementation Guide

The document provides a simple implementation of a cursor in SQL using a Student table. It includes the creation of the table, insertion of sample data, and the definition and usage of a cursor to fetch and print student names. Finally, it demonstrates how to close and deallocate the cursor after use.

Uploaded by

sighking789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

You might also like