Solved

Can I pass a string variable to Cursor?

Posted on 2002-03-25
3
557 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

937 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now