Create cursor using dynamic table name | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Create cursor using dynamic table name

Is it possible to create cursour by using the dynamic temp table name.
e.q.
DECLARE authors_cursor CURSOR
FOR SELECT * FROM @DynamicTempTable
OPEN authors_cursor Surendra Kalekar
Yes possible only if every statement is in a single block
Declare @table varchar(20)
Declare @sql varchar(2000)
Set @table=’tableName’
Set @sql=’DECLARE authors_cursor CURSOR
FOR SELECT * FROM ‘[email protected]+’OPEN authors_cursor
–Do whatever you want
Deallocate authors_cursor’ Madhivanan Failing to plan is Planning to fail
quote:Originally posted by surendrakalekar Is it possible to create cursour by using the dynamic temp table name.
e.q.
DECLARE authors_cursor CURSOR
FOR SELECT * FROM @DynamicTempTable
OPEN authors_cursor Surendra Kalekar

Hi surendra,
did u mean cursor for table variable or cursor for temp table
or cursor for table name stored in a variable.
because all is possible.
quote:Originally posted by Madhivanan Yes possible only if every statement is in a single block
Declare @table varchar(20)
Declare @sql varchar(2000)
Set @table=’tableName’
Set @sql=’DECLARE authors_cursor CURSOR
FOR SELECT * FROM ‘[email protected]+’OPEN authors_cursor
–Do whatever you want
Deallocate authors_cursor’ Madhivanan Failing to plan is Planning to fail
Thanks Madhivana, You mean all the statements related to cursors including the while loop I have to put into single quote ‘ ‘ and then execute it.
Surendra Kalekar
quote:Originally posted by ranjitjain
quote:Originally posted by surendrakalekar Is it possible to create cursour by using the dynamic temp table name.
e.q.
DECLARE authors_cursor CURSOR
FOR SELECT * FROM @DynamicTempTable
OPEN authors_cursor Surendra Kalekar

Hi surendra,
did u mean cursor for table variable or cursor for temp table
or cursor for table name stored in a variable.
because all is possible.
I mean … cursor for table name stored in a variable and that name is dynamic e.q. the temp table name will be ‘#’ + @SessionID. so it will be different each time. Surendra Kalekar
quote:Thanks Madhivana, You mean all the statements related to cursors including the while loop I have to put into single quote ‘ ‘ and then execute it.
Yes
Madhivanan Failing to plan is Planning to fail
Seehttp://www.sommarskog.se/dynamic_sql.html
SQL Server MVP Erland Sommarskog dedicated a short chapter to this topic —
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de
Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)

]]>