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 777327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:35:20+00:00 2026-05-14T19:35:20+00:00

I am trying to set some id’s for a bunch of rows in a

  • 0

I am trying to set some id’s for a bunch of rows in a database where the id column is an identity.

I’ve created a cursor to loop through the rows and update the ids with incrementing negative numbers (-1,-2,-3 etc).

When I updated just one row turning on the IDENTITY_INSERT it worked fine but as soon as I try and use it in a cursor, it throws the following error.

Msg 8102, Level 16, State 1, Line 22
Cannot update identity column ‘myRowID’.

DECLARE @MinId  INT;
SET @MinId = (SELECT MIN(myRowId) FROM myTable)-1;

DECLARE myCursor CURSOR
FOR
SELECT myRowId
FROM dbo.myTable
WHERE myRowId > 17095

OPEN myCursor 
DECLARE @myRowId INT

FETCH NEXT FROM myCursor INTO @myRowId
WHILE (@@FETCH_STATUS <> -1)
BEGIN

SET IDENTITY_INSERT dbo.myTable ON;

--UPDATE dbo.myTable
--SET myRowId = @MinId
--WHERE myRowId = @myRowId;

PRINT (N'ID: ' + CAST(@myRowId AS VARCHAR(10)) + N' NewID: ' + CAST(@MinId AS VARCHAR(4)));
SET @MinId = @MinId - 1;
FETCH NEXT FROM myCursor INTO @myRowId
END

CLOSE myCursor 
DEALLOCATE myCursor 
GO
SET IDENTITY_INSERT dbo.myTable OFF;
GO

Does anyone know what I’m doing wrong?

  • 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-14T19:35:21+00:00Added an answer on May 14, 2026 at 7:35 pm

    You wouldn’t need a cursor for this anyway. Ignoring that they are identity columns something like this would work in a derived table then you could join onto it to update all rows in a set based way

    select 0-row_number() over( order by myRowId asc) as myRowId,*
     from dbo.myTable
    WHERE myRowId > 17095
    

    This still might be a useful approach if you end up setting identity insert on then inserting them all like that then deleting ones WHERE myRowId > 17095 (in that order!) in a transaction

    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE 
    
    BEGIN TRAN
    SET IDENTITY_INSERT dbo.myTable ON;
    
    INSERT INTO dbo.myTable
    SELECT 0-row_number() OVER( ORDER BY myRowId ASC) AS myRowId, OtherColumns
     FROM dbo.myTable
    WHERE myRowId > 17095
    
    DELETE FROM dbo.myTable WHERE myRowId > 17095
    
    SET IDENTITY_INSERT dbo.myTable OFF;
    COMMIT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm iterating through some objects and trying to set some properties that, apparently, don't
I'm trying to set some values to my NSMutableDictionary inside a loop and assigning
I am trying to set up some jUnit testing. Our database is connected by
I'm trying to set up some database tables and create some model classes for
I'm trying to set some javascript data (json) in my markup by calling a
I am trying to set some text on a label descriptionLabel.text = [NSString stringWithFormat:mySTUser.bio];
I am trying to set some default inherited permissions to a directory that will
I'm trying to set some custom AWS CloudWatch metrics using the Java SDK. I
In my ruby(1.9.3) on rails (3.1) app I'm trying to set some conditional behavior
I'm using Extjs 3.4. When some details are loaded, I'm trying to set some

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.