T-SQL Tutorial

Open cursors - TSQL Tutorial


In this page you can learn how to open an cursor by reading the syntax and the example.

Open cursor Syntax:

OPEN { { cursor_name } | cursor_variable_name }





Open cursor example:

USE model;
GO
DECLARE Student_Cursor CURSOR FOR
  SELECT id, first_name, last_name, country
FROM dbo.students WHERE country != 'US';
OPEN Student_Cursor;
FETCH NEXT FROM Student_Cursor;
WHILE @@FETCH_STATUS = 0
     BEGIN
     FETCH NEXT FROM Student_Cursor;
   END;
CLOSE Student_Cursor;
DEALLOCATE Student_Cursor;
GO

idfirst_namelast_namecountry
3SophiaDAVISFrance
4EmmaEVANSGermany
6DavidBROWNEngland