Click or drag to resize

SA0258 : The number of FETCH statement variables does not match the number of columns in the cursor definition

The topic describes the SA0258 analysis rule.

Message

The number of FETCH statement variables does not match the number of columns in the cursor definition

Description

The rule checks T-SQL code for FETCH cursor statements having an INTO clause, which has a number of variable different form the number of columns in the cursor declaration select list.

The INTO clause of the FETCH cursor statement allows data from the columns of a fetch to be placed into local variables.

Each variable in the list, from left to right, is associated with the corresponding column in the cursor result set, and the number of variables must match the number of columns in the cursor select list.

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.

Categories

Design Rules, New Rules

Additional Information
Example Test Script
SQL
 1declare @col1 int, @col2 int, @col3 int
 2DECLARE test1 CURSOR
 3    FOR SELECT col1,col2,col3 FROM Table1
 4
 5OPEN test1
 6FETCH NEXT FROM test1
 7 INTO @col1, @col2
 8
 9FETCH NEXT FROM test1
10 INTO @col1, @col2, @col3
11
12DECLARE test2 CURSOR
13    FOR SELECT col1,col2,col3,t.* FROM Table1 t
14
15OPEN test2
16FETCH NEXT FROM test2
17 INTO @col1, @col2,@col3

Analysis Results

 MessageLineColumn
1SA0258 : The number of FETCH statement variables is 2, while the number of columns in the cursor definition is 3.71
See Also

Other Resources