Update a column with sequence number in SQL Server

We keep an unique column in every table in the database to have primary key clustered index created on it and mostly they are IDENTITY or ROWGUID. Have you ever faced a situation where you need to update a column with sequence number in an existing table or temp table? I will explain 4 different methods here to achieve the same.

SETUP:

Method 01: (Using IDENTITY)

Method 02: (Using local variable)

Method 03: (Using CTE)

Method 04: (Using SEQUENCE)

 

Prasad Sahoo

Prasad Sahoo, is an enthusiast of Microsoft technologies in general and a passionate database professional, mainly focusing on SQL Server performance tuning and business intelligence. He has contributed a long time of his career working on SQL Server and other RDBMS. If he is not busy with SQL stuff then he must be watching some Hollywood movies.

One thought on “Update a column with sequence number in SQL Server

  • May 31, 2017 at 3:23 pm
    Permalink

    Hi Prasad,

    Thanks a lot for your posts. i was just looking for some good stuff in sql and i got this link. I am to say, your link complete my search.

    Thanks a lot for a such good knowledge.

    Regards
    Ashish Jain

    Reply

Leave a Reply

%d bloggers like this: