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

x
?
Solved

Cursor read-Only Error

Posted on 2011-02-28
3
Medium Priority
?
707 Views
Last Modified: 2012-05-11
     
The following code gives an error of "cursor is read only".
The problem is caused by the order by clause of the Cursor select definition.
Is there a fix for this scenario.
Typically the table will contain records with Batch values of 100,120, 139, 200 ..etc and I wish to
update the Batch values as 10,20,30,40 ..etc  


                declare @n smallint
      declare @batch smallint
      DECLARE cursor1 CURSOR SCROLL_LOCKS
      FOR
      SELECT     Batch
      FROM       JazzCarryOver
      order by Batch FOR UPDATE OF Batch;

      OPEN cursor1;

        set @n = 10
      FETCH NEXT FROM Cursor1 into @batch
-- re number Batch in ascending order
      WHILE @@FETCH_STATUS = 0
      BEGIN
            UPDATE JazzCarryOver
            SET Batch = @n
            WHERE CURRENT OF cursor1;
            set @n = @n + 10
            FETCH NEXT FROM Cursor1 into @lotnum,@batch
    END;
      CLOSE Cursor1;
      DEALLOCATE Cursor1;

0
Comment
Question by:alcindor
[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 12

Assisted Solution

by:HugoHiasl
HugoHiasl earned 400 total points
ID: 35002369
updating a row in a cursor where multiple rows are aggregated by using a group by clause is not possible.

What would you expect the update to do? Updating ALL relevant rows or updating only the FIRST or LAST? Or one in the middle?

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1600 total points
ID: 35002420
you dont really need a cursor here , use the following

declare @n int = 0
update JazzCarryOver
set @n = @n+10,
   batch = @n
0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 35003237
Thanks guys,
0

Featured Post

Independent Software Vendors: 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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

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