CLOSE (Transact-SQL)
Closes a cursor.
Syntax
CLOSE
[ cursor_name ]
Parameters
- cursor_name
- The name of the cursor to be closed. If not supplied, the cursor currently opened is closed.
Description
The CLOSE
statement closes the specified cursor. Closing a cursor deallocates the current data structure that holds the result set of the cursor. Any locks held by the cursor are released.
After a cursor is closed, it can be reopened using the OPEN
statement. You must close a cursor before it can be dropped.
Permissions
CLOSE
permissions default to any valid user. Permissions required for other tasks can be granted to a role.
Examples
Example 1: Closing a specific cursor
This example assumes that a cursor named my_cursor
has been declared and opened.
DECLARE my_cursor CURSOR FOR
SELECT column_name FROM table_name;
OPEN my_cursor;
-- Fetch rows from the cursor ...
CLOSE my_cursor;
DEALLOCATE my_cursor;
Example 2: Closing the current cursor
If you are inside a stored procedure or trigger and only have one active cursor, you can omit the cursor name.
-- Assume a cursor is already opened
FETCH NEXT FROM cursor_name INTO @variable;
CLOSE; -- Closes the current cursor