Link to home
Start Free TrialLog in
Avatar of meimeius
meimeius

asked on

Can I pass a string variable to Cursor?

I have a stored procedure as below.  I’d like to pass "cursor scroll dynamic for select Name from Table1 order by Name asc" as varible.  My code used to be like this "set @cursor = cursor scroll dynamic for select Name from Table1 order by Name asc", after I change to "set @cursor =  @SQL", it doesn’t work.

My @SQL = "cursor scroll dynamic for select Name from Table1 order by Name asc".

CREATE PROCEDURE uspCursor
--@SQL varchar(2000),
@Count int,
@StartPoint int,
@Chunk int,
@ReturnIndex int

AS

declare @Cursor     Cursor
declare @Name varchar(50)

set @cursor = cursor scroll dynamic for select Name from Table1 order by Name asc
--set @cursor =  @SQL
open @cursor
fetch absolute @StartPoint from @cursor into @Name

while (@@fetch_status = 0 and @ReturnIndex = 1) or
      (@@fetch_status = 0 and @ReturnIndex = 0 and @Count < @Chunk)
begin
     print 'Name: ' + cast(@Name as varchar(19))
     
     
     set @Count = @Count + 1

     if (@ReturnIndex = 1)
          fetch relative @Chunk from @cursor into @Name
     else
          fetch next from @cursor into @Name
end
close @cursor
Avatar of Syed Irtaza Ali
Syed Irtaza Ali
Flag of Pakistan image

the reason is that
The fetch type Absolute cannot be used with dynamic cursors.

ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meimeius
meimeius

ASKER

Thank you very much!