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

  • Home
  • SEARCH
  • 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 7039665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:33+00:00 2026-05-28T01:49:33+00:00

My code seems pretty forward. I want to update a specific field with a

  • 0

My code seems pretty forward.

I want to update a specific field with a unique counter, not equal {1,2,3,…}.

I keep getting the error ‘The cursor is READ ONLY.’

Also: is there a simpler way?

declare @MaxVal int = NULL
declare @fetchVal int = NULL
select @MaxVal = MAX(tp_Id)+1 from [<tableContainingInitialMaxval>] 
/** some default **/

DECLARE curs01 CURSOR 
for select @maxVal + row_number() OVER (order by [<someUniqueField>]) from [<table2update>];
(used FOR UPDATE OF [<field2update>] but that made no difference)

open curs01
FETCH NEXT FROM curs01 INTO @fetchVal;
WHILE @@FETCH_STATUS = 0
    begin
        update [<table2update>] set [<field2update>] =  @fetchVal 
        WHERE CURRENT OF curs01;
        FETCH NEXT FROM curs01 INTO @fetchVal;
    end;
CLOSE curs01;
DEALLOCATE curs01;
GO 
  • 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-28T01:49:34+00:00Added an answer on May 28, 2026 at 1:49 am

    You don’t need a cursor for this.

    DECLARE @MaxVal INT = NULL
    
    SELECT @MaxVal = MAX(tp_Id) + 1
    FROM   tableContainingInitialMaxval;
    
    WITH CTE
         AS (SELECT *,
                    @maxVal + row_number() OVER (ORDER BY someUniqueField) AS rn
             FROM   table2update)
    UPDATE CTE
    SET    field2update = rn  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code seems not working. // $counter is an instance of Zend_Db_Table_Abstract $counter->update(array('hits' =>
My code seems to run but it does not display the result any result
The following code seems to be just too much, for getting a single count
The following code seems not to work, even though the file appears to be
I am writing the following code on boost's program_options (version 1.42). This seems straight-forward
My question seems pretty complicated to me (maybe not to you guys) so I
This seems to be pretty straight forward. I need to send email from some
The concept of narrowing seems pretty straight-forward. However, could someone please explain why some
My code seems to do infinite recursion when I invoke it with trees of
The below code seems like it should work; however, the blob in the database

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.