我已多次检查过这个问题。列数匹配,但仍然会出现以下错误:
Cursorfetch:INTO列表中声明的变量数必须为 与所选列的匹配。
我知道这个问题已被多次询问过,但我仍然陷入困境。任何帮助将不胜感激。感谢。
DECLARE @prev_s_Case CHAR(4)= null,@prev_startDate DATETIME= null,@prev_endDate DATETIME= null, @prev_ukey INT= null, @cur_s_Case CHAR(4), @cur_startDate DATETIME, @cur_endDate DATETIME, @cur_ukey INT, @result varchar(500);
DECLARE THE_CURSOR CURSOR FOR
SELECT s_Case, startDate, endDate, ukey FROM #TEMP
ORDER BY s_Case
BEGIN
OPEN THE_CURSOR;
FETCH NEXT FROM THE_CURSOR
INTO @cur_s_Case, @cur_startDate, @cur_endDate, @cur_ukey
WHILE (@@FETCH_STATUS=0)
SELECT s_Case, startDate, endDate, ukey
FROM #TEMP
IF ( (@prev_s_Case = @cur_s_Case))
BEGIN
IF ((@cur_startDate BETWEEN @prev_startDate AND @prev_endDate) AND (@cur_endDate >= @prev_endDate))
BEGIN
SET @result = CASE WHEN @result = '' THEN str(@prev_ukey)
ELSE @result + ','+ str(@prev_ukey) END
END
ELSE IF ((@cur_startDate BETWEEN @prev_startDate AND @prev_endDate)
AND (@cur_endDate < @prev_endDate))
BEGIN
SET @result = CASE WHEN @result = '' THEN str(@cur_ukey)
ELSE @result + ','+ str(@cur_ukey) END
END
END
SET @prev_s_Case= @cur_s_Case;
SET @prev_startDate = @cur_startDate;
SET @prev_endDate = @cur_endDate;
SET @prev_ukey = @cur_ukey;
FETCH NEXT FROM THE_CURSOR
INTO @cur_s_Case,@cur_startDate,@cur_endDate,@cur_ukey
print @result
CLOSE THE_CURSOR;
DEALLOCATE THE_CURSOR;
END