Solved

Cursor read-Only Error

Posted on 2011-02-28
3
699 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 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
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

734 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