我正在 Sql Server 2008 中构建一个存储过程。存储过程有两个参数,它们是表的列名。在存储过程中,我使用动态查询在 Cursor 的帮助下获取这两列的数据。
代码:
Create PROCEDURE TestSP
(
@firstAttribute nvarchar(max),
@secondAttribute nvarchar(max)
)
AS
DECLARE @x FLOAT
DECLARE @y INT
DECLARE @query nvarchar(max)
DECLARE @cursor_query nvarchar(max)
DECLARE @result_Cursor as cursor
BEGIN
SET @query = 'Select '+ @firstAttribute+','+@secondAttribute+' from TBL_TEST_DATA_NEW'
SET @cursor_query =' set @cursor = cursor for ' + @query +' open @cursor;'
PRINT 'CURSOR_QUERY'+@cursor_query
exec sys.sp_executesql
@cursor_query
,N'@cursor cursor output'
,@result_Cursor output
FETCH NEXT FROM result_Cursor INTO @x, @y
但是当我执行这个 SP 时,它给了我以下错误
Msg 16916, Level 16, State 1, Procedure TestSP, Line 33
A cursor with the name 'result_Cursor' does not exist.
执行命令:
Exec TestSP "Column_1","Column_2"
有人可以告诉我为什么会收到此错误
请帮忙..
谢谢