Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Building dynamic cursor statements

Author  Topic 

shakana
Starting Member

6 Posts

Posted - 2004-09-08 : 20:22:35
Is it possible to use a variable when declaring a cursor in a stored procedure? The following code is the concept I'm trying to accomplish.

Example
Declare @sql varchar(2000)
Declare @tablename varchar(255)
set @tablename = "temp"
set @sql = "Select * from " + @tablename
DECLARE blkFS_cursor
CURSOR FOR @sql

When I try this in QueryAnalyzer, I receive the following error:
Line 6: Incorrect syntax near '@sql'.

-Andre

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-09-08 : 20:26:33
No, that isn't possible. Why do you need a dynamic cursor?

Tara
Go to Top of Page

shakana
Starting Member

6 Posts

Posted - 2004-09-08 : 20:30:22
I need a way to access a table which name I build dynamically. (I'm trying to get aroung using temp tables and granting sysadmin rights). I this case, I can build a select statement and use the Exec command but then I wouldn't have access to the result set. So I thought cursors would be an alternative.

-Andre
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-09-08 : 20:32:08
You can put the results of a dynamic statement into a temp table:

INSERT INTO #Temp (Column1, Column2)
EXEC (dynamic SQL goes here)

Tara
Go to Top of Page
   

- Advertisement -