SET @Str='1,2,3,4'
DECLARE Cur CURSOR FOR
EXEC('SELECT [Id] FROM [List] WHERE [Id] NOT IN('+@Str+') ORDER BY [Id] DESC')
--出错原因:EXEC附近有语句错误,在游标中不能用EXEC吗?
OPEN SntCur
FETCH NEXT FROM Cur INTO @Id
WHILE @@FETCH_STATUS=0
BEGIN
--此处省略...
FETCH NEXT FROM Cur INTO @Id
END
CLOSE Cur
DEALLOCATE Cur
------解决方案--------------------
如果是学方法,可以这样:
DECLARE @SqlT VARCHAR(200),@Str VARCHAR(20),@Id INT
SET @Str='1,2,3,4'
SET @SqlT='DECLARE SntCur CURSOR FOR
SELECT [Id] FROM [List] WHERE [Id] NOT IN('+@Str+') ORDER BY [Id] DESC)'
EXEC(@SqlT)
--出错原因:EXEC附近有语句错误,在游标中不能用EXEC吗?
OPEN SntCur
FETCH NEXT FROM Cur INTO @Id
WHILE @@FETCH_STATUS=0
BEGIN
--此处省略...
FETCH NEXT FROM Cur INTO @Id
END
CLOSE SntCur
DEALLOCATE SntCur