开发者

Is it possible to pass a cursor variable to sp_executesql?

Despite declaring a variable as type "cursor" when passing it to sp_executesql, I get the error "Operand type clash: nvarchar is incompatible with cursor".

declare CURSOR_TO_PASS cursor for... --a simp开发者_C百科le select statement
--cursor opened, values obtained, etc...
declare @item nvarchar(5);
declare @seqno int;
--@item and @seqno populated
declare @sql nvarchar(400) = N'update MYTABLE set Survey' + cast(@seqno as nvarchar(2)) + N' = @itemvalue where current of @sc';
exec sp_executesql @sql, N'@itemvalue nvarchar(5), @sc cursor', @itemvalue = @item, @sc = CURSOR_TO_PASS;

I don't know what's wrong, because I've declare @sc as a cursor, and CURSOR_TO_PASS is a cursor, which I'm assigning to @sc when calling sp_executesql. So, is it possible to pass a cursor to sp_executesql?


I figured out the solution shortly after posting this. It is in fact possible to pass a cursor variable. There's just an intermediate step required where you have to assign the cursor to a "cursor variable", as demonstrated here: http://msdn.microsoft.com/en-us/library/ms190028.aspx "A cursor can be associated with a cursor variable by either of two methods:", both of which require an initial, basic cursor variable declaration like "DECLARE @MyVariable CURSOR;".

So I added these lines to my code:

declare @cursorvariable cursor;
set @cursorvariable = CURSOR_TO_PASS;

Then I changed @sc = CURSOR_TO_PASS to @sc = @cursorvariable, and it worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜