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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much!
The fetch type Absolute cannot be used with dynamic cursors.