IN SLQServer 2012 - SQL Server Management Studio
Modifying a stored procedure that has been running forever using one variable for the cursor. I'm trying to use
at least 5 variables for the cursor but get this error when I go over 2 variables.
Using cursors of 1 or 2 variables is no problem no matter what name or what order they are in.
But When trying more than 2 variables I get the error.
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
I have tried interchanging all different names and combinations that work together as 2 variables so
I know it's not due to type mismatch. But when I add one that works in combination as 2 variables in the code
as a 3rd variable I get the error. Same goes for 4 or 5 or 6 etc variables.
PROJECT_ID,PROJ_TYPE_ID,CLIENT_ID of TPROJECT are all of type integer in the table
What am I missing?
BEGIN TRY
-- Start with return flag set to Success.
SET @SuccessFlag = 0
DECLARE @ProjectID INT = NULL
DECLARE @ProjTypeID INT = NULL
DECLARE @ClientID INT = NULL
DECLARE @RecoveryID INT = NULL
DECLARE @ProjectName VARCHAR(255) = NULL
DECLARE Project_Cursor CURSOR FOR
SELECT PROJECT_ID,PROJ_TYPE_ID,CLIENT_ID
FROM TPROJECT
WHERE PROJECT_ID > 0 AND PROJ_STAT_AVT_ID IN (124, 123, 126, 125, 118)
-- Open the recordset
OPEN Project_Cursor;
-- Start with the first Id
FETCH NEXT FROM Project_Cursor INTO @ProjectID, @ProjTypeID, @ClientID;
-- Loop through the recordset
DECLARE Project_Cursor CURSOR LOCAL FAST_FORWARD FOR
SELECT PROJECT_ID,PROJ_TYPE_ID,CL
FROM TPROJECT
WHERE PROJECT_ID > 0 AND PROJ_STAT_AVT_ID IN (124, 123, 126, 125, 118)
OPEN Project_Cursor;
WHILE 1 = 1
BEGIN
FETCH NEXT FROM Project_Cursor INTO @ProjectID, @ProjTypeID, @ClientID;
IF @@FETCH_STATUS <> 0
IF @@FETCH_STATUS = -1
BREAK
ELSE
CONTINUE;
/*process cursor rows here*/
END /*WHILE*/
DEALLOCATE Project_Cursor