Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6928253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:09:45+00:00 2026-05-27T11:09:45+00:00

I am trying to update a row inside a cursor. What I am trying

  • 0

I am trying to update a row inside a cursor. What I am trying to do is update a chain of records with OLD_QTY and NEW_QTY. However when I try to do my update it gives the error The cursor is READ ONLY even though I included for update of OLD_QTY, NEW_QTY in my declration. It makes no difference if I include OLD_QTY and NEW_QTY in the select statement.

declare @current_inv_guid uniqueidentifier
declare @last_inv_guid uniqueidentifier
declare @current_vid int
declare @last_vid int
--declare @current_new_qty money
declare @last_new_qty money
--declare @current_old_qty money

declare iaCursor cursor 
      for select INV_GUID, old_VID
          --, OLD_QTY, NEW_QTY  
          from #IA 
          order by INV_GUID, old_vid, ENTRY_NUM 
      for update --of OLD_QTY, NEW_QTY
open iaCursor
Fetch next from iaCursor into @current_inv_guid, @current_vid --, @current_old_qty, @current_new_qty

while @@fetch_status = 0
begin
    --test to see if we hit a new chain.
    if(@last_inv_guid <> @current_inv_guid or @current_vid <> @last_vid)
    begin
        set @last_new_QTY = (select #lots.QTY_RECEIVED from #lots where #lots.INV_GUID = @current_inv_guid and LOT_VID = @current_vid)
        set @last_inv_guid = @current_inv_guid
        set @last_vid = @current_vid    
    end

    --update the current link in the chain
    update #ia
        set OLD_QTY = @last_new_QTY,
            NEW_QTY = @last_new_QTY + QTY_CHANGE,
            @last_new_QTY = @last_new_QTY + QTY_CHANGE
        where current of iaCursor

    --get the next link
    fetch next from iaCursor into @current_inv_guid, @current_vid --, @current_old_qty, @current_new_qty
end 

close iaCursor
deallocate iaCursor
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:09:45+00:00Added an answer on May 27, 2026 at 11:09 am

    Besides the reason you mentioned in your answer, what you’re attmepting to do runs counter to the way SQL is meant to be used. Try to update the data in sets, not by rows.

    I’m not positive, as I don’t know your table design, but I believe the following should work. You may get better performance out of this. In particular, I’m assuming that QTY_CHANGE is coming from #ia, although this may not be the case.

    UPDATE #ia as a set (OLD_QTY, NEW_QTY) = (SELECT #lots.QTY_RECEIVED + (COUNT(b.*) * a.QTY_CHANGE), 
                                                     #lots.QTY_RECEIVED + ((COUNT(b.*) + 1) * a.QTY_CHANGE)
                                              FROM #lots
                                              LEFT JOIN #ia as b
                                              ON b.INV_GUID = a.INV_GUID
                                              AND b.OLD_VID = a.OLD_VID
                                              AND b.ENTRY_NUM < a.ENTRY_NUM
                                              WHERE #lots.INV_GUID = a.INV_GUID
                                              AND #lots.LOT_VID = a.OLD_VID)
    WHERE EXISTS (SELECT '1'
                  FROM #lots
                  WHERE #lots.INV_GUID = a.INV_GUID
                  AND #lots.LOT_VID = a.OLD_VID)
    


    EDIT:

    … the previous version of the answer was written with a DB2 perspective, although it would otherwise be db-agnostic. It also had the problem of using the same value of QTY_CHANGE for every row, which is unlikely. This should be a more idiomatic SQL Server 2008 version, as well as being more likely to output the correct answer:

    WITH RT AS (SELECT #IA.inv_guid, #IA.old_vid, #IA.entry_num,
                       COALESCE(MAX(#Lots.qty_received), 0) + 
                             SUM(#IA.qty_change) OVER(PARTITION BY #IA.inv_guid, #IA.old_vid 
                                                      ORDER BY #IA.entry_num) 
                            AS running_total                                      
                           FROM #IA
                           LEFT JOIN #Lots
                                  ON #Lots.inv_guid = #IA.inv_guid
                                     AND #Lots.lot_vid = #IA.old_vid)
    UPDATE #IA
    SET #IA.old_qty = RT.running_total - #IA.qty_change, #IA.new_qty = RT.running_total
    FROM #IA
    JOIN RT
      ON RT.inv_guid = #IA.inv_guid
         AND RT.old_vid = #IA.old_vid
         AND RT.entry_num = #IA.entry_num
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a row update from php to an mysql database. It
I'm trying to do a single row insert/update on a table but all the
I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException:
I am trying to update a specific row that uses auto_increment as a primary
I am trying to update a table in my database with another row from
I am trying to UPDATE a record if there is a row in the
I am trying to Update a row if it exists, otherwise insert (on duplicate
I am trying to update a row in a table which has no unique
I'm trying to update the particular row of a cellTable without loading the page
I'm currently trying to update a row using the sqlitedatabase.update method here: Heres the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.