我正在Sql Server 2008中構建一個存儲過程。存儲過程有兩個參數,它們是表的列名。在存儲過程中,我使用動態查詢在Cursor的幫助下獲取這兩列的數據。動態查詢不能在SqlServer 2008中使用遊標存儲過程
代碼:
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+','[email protected]+' from TBL_TEST_DATA_NEW'
SET @cursor_query =' set @cursor = cursor for ' + @query +' open @cursor;'
PRINT 'CURSOR_QUERY'[email protected]_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"
有人能告訴我爲什麼我得到這個錯誤
請幫助..
感謝
添加** ** @你的參數** ** result_Cursor,然後重試: 'NEXT FROM @result_Cursor提取到@x,@ y' –
它的工作...謝謝Nadeem ...)) – Abhay
你是最受歡迎的;) –