Recently I had to write a stored procedure in which I need to update the column in the table variable with its Row Number that too in the proper series.

And I got quite an awesome and simple query which might help the Database guys. Please note I have tested this on SQL Server 2008.

Declare @cnt bigint = 0

Now Suppose we have a table variable with the id column and its already populated with the data.

declare @tab table ( id bigint, first_name varchar(25), last_name varchar(25) )

Now the update query to update the id column with the row number in series. Please fire this once all the data is inserted in the table variable.

update @tab set @cnt = id = @cnt + 1

Now you all might be wondering how its working. Just give a thought and if you want you even give a try and it really works awesomely.

Waiting for your comments.

Share

Related Posts:

  • No Related Posts