Create a simple CURSOR in SQL server

Neeks
Posted by Neeks under Sql Server category on | Views : 13393
DECLARE @id INT --Declaring the Variable @id
DECLARE @getID CURSOR -- Declaring the Cursor
SET @getID = CURSOR --Assigning the cursor
FOR
SELECT intID FROM tbl_student --Query related to Cursor
OPEN @getID -- Opening the Created cursor
FETCH NEXT FROM @getID --Retrieving the record one by one
INTO @ID --Assigning the value in declared Variable
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @ID
FETCH NEXT
FROM @getID INTO @ID
END
CLOSE @getID -- Closing the Cursor
DEALLOCATE @getID -- Deallocating the Cursor Memory

Comments or Responses

Login to post response