Sunday, March 25, 2012

Cursor in SQL Server

Hi,
I've discovered the stored procedures lately, and moving almost all my requests to use this.

I've a question about declaring a cursor.

For example :
DECLARE @.strSQL varchar(255)
SELECT @.strSQL = 'SELECT au_lname FROM authors'

I want to declare the cursor like this
DECLARE csrAuthor
FOR @.strSQL
READ ONLY

I'm always getting an error. Is there a solution to make a "dynamic" cursor.

Thanks
FrankTry putting your DECLARE CURSOR statement inside an execute statement. Like this:

EXEC ('DECLARE Oprid_Cursor CURSOR FOR ' + @.VAR1)

Then you can OPEN the cursor etc.

Hope this helps.|||It might be possible to do something like this also but I haven't tried it:

DECLARE csrAuthor
FOR exec sp_executesql @.strSQL
READ ONLYsql

No comments:

Post a Comment