T-SQL Tutorial

Cursor - TSQL Tutorial


On Transact SQL language the Cursor is an cursor data type for variables or stored procedure OUTPUT parameters that contain a reference to a cursor.

Local variable cursor example

USE model;
GO
DECLARE cur_variable CURSOR
FOR SELECT * FROM courses;
OPEN cur_variable
FETCH NEXT FROM cur_variable;
GO

Stored procedure output parameters

USE model;
GO
CREATE PROCEDURE info_course
@varCur CURSOR VARYING OUTPUT
AS
SET NOCOUNT ON;
SET @varCur = CURSOR
FORWARD_ONLY STATIC FOR
SELECT id, name, price
FROM courses;
OPEN @varCur;
GO