Having difficulty opening a cursor from a dynamically created temp table.
I'm creating a temporary table based upon a processes @@SPID value. I then populate the table using sp_executesql.
How can I open a cursor based on the dynamically created temporary table?
I've opened it successfully by using code similar to below:
DECLARE @srtSQL VARCHAR(1024)
SET @strSQL = 'DECLARE @CURSOR CURSOR GLOBAL FOR @CURSOR SELECT * FROM ' + @TEMP_TABLE_NAME
EXECUTE sp_executesql @strSQL
This seems to work but the only way to continue using the cursor is to call it by building another string and executing it. If you try to access the @CURSOR the Stored Proceedure will not complile. Must declare the variable @CURSOR
Im looking for a way to do something like below.
DECLARE @CURSOR CURSOR
SET @CURSOR = CURSOR FOR SELET * FROM[/color] "DYNAMIC TEMP TABLE"
There has to be a way to do this.... Is there some sort of way to make an alias for the table name?
Maybe I'm taking the wrong approach to this whole thing. What I'm trying to avoid is the Stored Proceedure running twice at the same time. If I don't give the tables unique names I'll run into problems right? I say yes.
Any suggestions? I'm fairly new to SP writing so hopefully I'm doing a decent job describing what my issues are.
--Rick
I'm creating a temporary table based upon a processes @@SPID value. I then populate the table using sp_executesql.
How can I open a cursor based on the dynamically created temporary table?
I've opened it successfully by using code similar to below:
DECLARE @srtSQL VARCHAR(1024)
SET @strSQL = 'DECLARE @CURSOR CURSOR GLOBAL FOR @CURSOR SELECT * FROM ' + @TEMP_TABLE_NAME
EXECUTE sp_executesql @strSQL
This seems to work but the only way to continue using the cursor is to call it by building another string and executing it. If you try to access the @CURSOR the Stored Proceedure will not complile. Must declare the variable @CURSOR
Im looking for a way to do something like below.
DECLARE @CURSOR CURSOR
SET @CURSOR = CURSOR FOR SELET * FROM[/color] "DYNAMIC TEMP TABLE"
There has to be a way to do this.... Is there some sort of way to make an alias for the table name?
Maybe I'm taking the wrong approach to this whole thing. What I'm trying to avoid is the Stored Proceedure running twice at the same time. If I don't give the tables unique names I'll run into problems right? I say yes.
Any suggestions? I'm fairly new to SP writing so hopefully I'm doing a decent job describing what my issues are.
--Rick