Update Identity column

  • edwardwill (11/17/2014)


    The update was incomplete as well. The requirement was to update two rows, so the SQL should have read

    SET IDENTITY_INSERT dbo.Roles ON

    UPDATE dbo.Roles

    SET RoleID=-3

    WHERE RoleID=-2;

    UPDATE dbo.Roles

    SET RoleID=-2

    WHERE RoleID=-1;

    SET IDENTITY_INSERT dbo.Roles OFF

    or something like that. Of course, the UPDATE was going to fail anyway, so the point is moot.

    One UPDATE was enough for demo of the QotD.

    Igor Micev,My blog: www.igormicev.com

  • Igor Micev (11/17/2014)


    Why not?

    No reason at all, I was just curious as to what scenario might require it 🙂

  • Really nice question. Good way to get the brain moving on a Monday morning.

  • This question was pretty simple for me... only because I tried this method of updating the primary keys late last week. My solution was no where even as sophisticated as the above solutions. 🙂

  • Good question, to remind us that allowing an arbitrary insert doesn't mean update too.

  • twin.devil (11/17/2014)


    Igor Micev (11/17/2014)


    The Question came as a real case experience with a software developer, and I had to do the update in that way, ... so thought to share it.

    Thanks.

    +1000, So true and QotD is the best place to share it. 🙂

    +1 Thanks for the great question.



    Everything is awesome!

  • Nice question, and it could be tricky for anyone that has manually updated an identity value through the GUI. I've done this on occasion and it's not apparent that a new table is actually being created in the background.

    Aigle de Guerre!

  • Good Question and good answer. Thanks

    Not all gray hairs are Dinosaurs!

  • Great question, Igor, thanks.

  • Ok, good to know that, thanx.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Hello Igor,

    In the question, are you assuming the correct database has been selected from the drop down menu?

    Thanks,

    Tim

    The pain of Discipline is far better than the pain of Regret!

  • Excellent Question! Thanks for sharing!

    The SWITCH TO method to update identity column is new to me. Thanks for that too.

Viewing 12 posts - 16 through 26 (of 26 total)

You must be logged in to reply to this topic. Login to reply