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
...
...
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
...
...