Skip to content

Latest commit

 

History

History
73 lines (58 loc) · 2.65 KB

close-transact-sql.md

File metadata and controls

73 lines (58 loc) · 2.65 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom f1_keywords helpviewer_keywords dev_langs monikerRange
CLOSE (Transact-SQL)
CLOSE (Transact-SQL)
rwestMSFT
randolphwest
03/06/2017
sql
t-sql
reference
ignite-2024
CLOSE_TSQL
CLOSE
closing cursors
cursors [SQL Server], closing
CLOSE statement
TSQL
=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

CLOSE (Transact-SQL)

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance Fabricsqldb]

Closes an open cursor by releasing the current result set and freeing any cursor locks held on the rows on which the cursor is positioned. CLOSE leaves the data structures available for reopening, but fetches and positioned updates are not allowed until the cursor is reopened. CLOSE must be issued on an open cursor; CLOSE is not allowed on cursors that have only been declared or are already closed.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

CLOSE { { [ GLOBAL ] cursor_name } | cursor_variable_name }  

Arguments

GLOBAL
Specifies that cursor_name refers to a global cursor.

cursor_name
Is the name of an open cursor. If both a global and a local cursor exist with cursor_name as their name, cursor_name refers to the global cursor when GLOBAL is specified; otherwise, cursor_name refers to the local cursor.

cursor_variable_name
Is the name of a cursor variable associated with an open cursor.

Examples

The following example shows the correct placement of the CLOSE statement in a cursor-based process.

DECLARE Employee_Cursor CURSOR FOR  
SELECT EmployeeID, Title FROM AdventureWorks2022.HumanResources.Employee;  
OPEN Employee_Cursor;  
FETCH NEXT FROM Employee_Cursor;  
WHILE @@FETCH_STATUS = 0  
   BEGIN  
      FETCH NEXT FROM Employee_Cursor;  
   END;  
CLOSE Employee_Cursor;  
DEALLOCATE Employee_Cursor;  
GO  

See Also