Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can I pass a string variable to Cursor?

Posted on 2002-03-25
3
Medium Priority
?
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
Brendt Hess earned 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

610 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