Error on T-Sql in Cursor use

2019-03-06 15:51发布

问题:

in below is part of my store peocedure that used to reading data from a view of a table and processing on it , but that have a error and I want to know that... (in select statement appear 8 parameter and front of cursor appear 8 paramete but have Error!!!)

DECLARE CUR CURSOR FOR 
SELECT PkJobDoneDaily_Id
       , Fk_ExecCarInOwner
       , Fk_ActivityType
       , Fk_CostSrc
       , Fk_ShiftNum
       , Fk_WorkGroup
       , DetachNum
        TotalTime
FROM  dbo. ViewAccJobDoneDaily
WHERE Fk_JobDonePeriod = @Fk_JobDonePeriod and IsOk=1 AND IsDel=0 AND Fk_ActivityType=2

OPEN CUR
FETCH NEXT FROM CUR INTO @Fk_JobDoneDaily,@Fk_ExecCarInOwner,@Fk_ActivityType
    ,@Fk_CostSrc,@Fk_ShiftNum,@Fk_WorkGroup,@DetachNum,@TotalTime

Error: Msg 16924, Level 16, State 1, Line 52 Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

回答1:

There is a comma missing between the select columns DetachNum and TotalTime.

Remark: Maybe you should check too that your select column PkJobDoneDaily_Id has the same name with the variable @Fk_JobDoneDaily to avoid to be confused (names start with P vs. F).



回答2:

You've missed a comma:

SELECT PkJobDoneDaily_Id
   , Fk_ExecCarInOwner
   , Fk_ActivityType
   , Fk_CostSrc
   , Fk_ShiftNum
   , Fk_WorkGroup
   , DetachNum
    TotalTime --<-- Here

So instead of selecting 8 columns, you're selecting 7 columns, the last of which you've given an alias of TotalTime to.