Generating unique numbers in MSSQL

Sometimes when we working, we create temp tables to keeping some data. I just want to add another column and gave id numbers to this table and almost spend 2 hours.

I hope you can found easily this story,

I used

ALTER TABLE #cat2 ADD id int

to create a new column at table

DECLARE @id INT //declare a variable as int

SET @id = 0

UPDATE #categories

SET @id = id = @id +1 //increase temp id for being one more on the next

GO

--

--