2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 16:49:39 +02:00
|
|
|
A ``++FETCH++`` statement fails when the number of variables does not match the number of columns selected in the CURSOR definition.
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,sql]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
DECLARE c1 cursor FOR SELECT FirstName, LastName FROM customer;
|
|
|
|
OPEN c1;
|
|
|
|
FETCH NEXT FROM c1 INTO @Name; -- Noncompliant
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,sql]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
DECLARE c1 cursor FOR SELECT FirstName, LastName FROM customer;
|
|
|
|
OPEN c1;
|
|
|
|
FETCH NEXT FROM c1 INTO @FirstName, @LastName;
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Refactor this ``++FETCH++`` to select the same number of columns selected in "xxx".
|
|
|
|
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
* primary: ``++INTO++`` + column list in ``++FETCH++`` statement
|
|
|
|
* secondary: ``++SELECT++`` + column list in cursor
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== is related to: S3613
|
|
|
|
|
|
|
|
=== is related to: S3614
|
|
|
|
|
|
|
|
=== on 20 Jul 2017, 15:26:19 Pierre-Yves Nicolas wrote:
|
|
|
|
According to the Microsoft https://docs.microsoft.com/en-us/sql/t-sql/language-elements/fetch-transact-sql[reference for FETCH]:
|
|
|
|
|
|
|
|
____
|
|
|
|
The number of variables must match the number of columns in the cursor select list.
|
|
|
|
____
|
|
|
|
|
|
|
|
|
|
|
|
The code can be compiled with no issue, for example in a stored procedure. The following error is raised at runtime:
|
|
|
|
|
|
|
|
----
|
|
|
|
Msg 16924, Level 16, State 1, Line 61
|
|
|
|
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
|
|
|
|
----
|
|
|
|
|
|
|
|
=== on 7 Aug 2017, 15:03:24 Pierre-Yves Nicolas wrote:
|
|
|
|
\[~massimo.paladin] Why minor? Such code triggers a failure at execution time.
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|