Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql server script error

Status
Not open for further replies.

diembi

Programmer
Sep 22, 2001
238
ES
Hello,
I am trying to execute a dynamic query in a cursor but I am getting error: A cursor with the name 'MyCursor ' does not exist.. Any idea, please? Thanks in advance!! Smile | :)
This is the code:

DECLARE @TempSQL NVARCHAR(max)
DECLARE @id INT
DECLARE @action_day INT
DECLARE @TableName VARCHAR(50) = 'MYTABLE'
DECLARE @MyCursor CURSOR
DECLARE @vquery nvarchar(max)
DECLARE @vsql nvarchar(max)

SET @TempSQL = 'CREATE TABLE ' + @TableName + '(
id INT,
action_day INT NULL,
)'
EXEC sp_executesql @TempSQL

set @vquery = N'SELECT id, action_day FROM ' + @TableName
set @vsql = 'set @cursor = cursor forward_only static for ' + @vquery + ' open @cursor;'

exec sys.sp_executesql
@vsql
,N'@cursor cursor output'
,@MyCursor output
FETCH NEXT FROM @MyCursor INTO @id,@action_day
...
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top