10 November, 2010
2 Comments
1 category
If you have a table in a Microsoft SQL Server database with an integer column that contains rows and you want to update the existing rows to contain consecutive numbers, you can use the following T-SQL query:
declare @COUNTER as int set @COUNTER = 0 -- First number will be [1] -- Update the [Number] column, so it contains consecutive numbers update [TestTable] set [Number] = @COUNTER, @COUNTER = @COUNTER + 1
Tags: SQL ServerT-SQL
Category: Uncategorized
Another method to provide row numbers for a table would be:
ALTER TABLE [TestTable] ADD [RowNumber] INTEGER IDENTITY
I had fun discovering that method. *wry grin*
Hi;
Would you be so kind to expand on the methodology above for example it is “looping through the rows” but how does it know?
Thanks
Simon