DF020: The cursor has not been declared.

Last modified: December 25, 2024

The topic describes the DF020 T-SQL code analysis rule.

Category

EXECUTION RULES

Message

The cursor has not been declared.

Description

All statements that reference the cursor by name must have the preceding DECLARE CURSOR statement.

Additional information

Following this rule helps prevent errors and ensures that cursors are correctly defined and available for use throughout the scope where they are declared. Failure to adhere to this requirement may result in syntax errors or unexpected behavior during query execution.

Noncompliant code example

OPEN cur
FETCH NEXT FROM cur INTO @CustomerId

Compliant solution

DECLARE cur CURSOR LOCAL FORWARD_ONLY FOR SELECT
  CustomerID
FROM dbo.Customer
OPEN cur
FETCH NEXT FROM cur INTO @CustomerId