Solved

Can I pass a string variable to Cursor?

Posted on 2002-03-25
3
560 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:meimeius
3 Comments
 
LVL 2

Expert Comment

by:Syed Irtaza Ali
ID: 6896315
the reason is that
The fetch type Absolute cannot be used with dynamic cursors.

0
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
ID: 6897492
You can do this with a Global cursor and an EXEC statement, something like this:

Exec('Declare MyCursor cursor GLOBAL scroll dynamic for select Name from Table1 order by Name asc')

You can now work with the cursor MyCursor:

Open Global MyCursor

fetch absolute @StartPoint from MyCursor 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 MyCursor into @Name
    else
         fetch next from MyCursor into @Name
end
close GLOBAL MyCursor
Deallocate Global MyCursor
0
 

Author Comment

by:meimeius
ID: 6897563
Thank you very much!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question