Home >> FAQs/Tutorials >> SQL Server FAQ

SQL Server FAQ - "OPEN" - Executing the Query of a Cursor

By: FYIcenter.com

(Continued from previous topic...)

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

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 FyiCenterData;
GO

DECLARE fyi_cursor CURSOR FOR
  SELECT * FROM fyi_links;
OPEN fyi_cursor;
-- result set is ready to use

-- other statements

CLOSE fyi_cursor;
DEALLOCATE fyi_cursor;
GO

(Continued on next topic...)

  1. What Are Cursors?
  2. How To Declare a Cursor with "DECLARE ... CURSOR"?
  3. How To Execute the Cursor Queries with "OPEN" Statements?
  4. How To Fetch the Next Row from a Cursor with a "FETCH" Statement?
  5. How To Transfer Data from a Cursor to Variables with a "FETCH" Statement?
  6. How To Loop through the Result Set with @@FETCH_STATUS?
  7. How To Declare and Use Cursor Variables?
  8. How To Create a Scrollable Cursor with the SCROLL Option?
  9. How To Create a Dynamic Cursor with the DYNAMIC Option?

Related Articles:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...