Solved

Cursor read-Only Error

Posted on 2011-02-28
3
692 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
3 Comments
 
LVL 12

Assisted Solution

by:HugoHiasl
HugoHiasl earned 100 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 400 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 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