SA0256 : A cursor with the same name is declared earlier. Avoid reusing cursor names

The topic describes the SA0256 analysis rule.

Message

A cursor with the same name is declared earlier. Avoid reusing cursor names

Description

The rule checks T-SQL code for cursor declarations and reports when a cursor name is declared more than once in the current scope.

It is not a good practice to re-declare cursor and should be avoided.

How to fix

Use different cursor name for cursors declared in the same scope.

Rename the cursor in the new cursor declaration, find all cursor references occurring after the new cursor declaration and update them with the new cursor name.

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
8 minutes per issue.
Categories

Design Rules, Bugs

Additional Information
Example Test SQL
SQL
 1DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor
 2
 3FETCH NEXT FROM vend_cursor
 4
 5OPEN vend_cursor
 6
 7CLOSE vend_cursor
 8
 9DEALLOCATE vend_cursor
10
11DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor

Analysis Results
  Message Line Column
1 SA0256 : The declared cursor name [vend_cursor] is used in a previous declaration. Avoid reusing cursor names. 11 8
See Also

Other Resources