Skip to content

DEALLOCATE

DECLARE @MyCursor CURSOR;
SET @MyCursor = CURSOR FOR
SELECT EmployeeID, FirstName, LastName
FROM Employees;
OPEN @MyCursor;
CLOSE @MyCursor;
DEALLOCATE @MyCursor;
Command(s) completed successfully.

In this example, a cursor named @MyCursor is declared and set to a select statement querying EmployeeID, FirstName, & LastName from the Employees table.

The OPEN command opens the cursor to use for data manipulation.

The CLOSE command closes the cursor, notifying SQL Server that resources associated with the cursor can no longer be accessed.

Finally, the DEALLOCATE command is used to remove the cursor definition and resources associated with it.

After deallocating a cursor, it no longer exists and cannot be closed or deallocated again. Trying to do so will result in an error message. Hence, the output message in successful scenarios will be ‘Command(s) completed successfully’ after DEALLOCATE.