MS SQL Server Concepts and Programming Question:

Download Job Interview Questions and Answers PDF

How To Execute the Cursor Queries with "OPEN" Statements?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

Once a cursor is declared, you need to execute the query attached to the cursor so that the result set returned from the query can be accessed through the cursor. To execute the cursor query, you should use the OPEN statement as in this format:

OPEN cursor_name;

When you are done with using the result set attached to a cursor, you should close the result set to free up server resources.

The tutorial example below shows you how to open and close a cursor:

USE GlobalGuideLineDatabase;
GO

DECLARE ggl_cursor CURSOR FOR
SELECT * FROM ggl_links;
OPEN ggl_cursor;
-- result set is ready to use

-- other statements

CLOSE ggl_cursor;
DEALLOCATE ggl_cursor;
GO


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
How To Declare a Cursor with "DECLARE ... CURSOR" in MS SQL Server?How To Fetch the Next Row from a Cursor with a "FETCH" Statement?