July 31, 2002 at 7:57 am
I have added a new column in a table. I would like to sequentially number each record. Is there a way to do this with a T-SQL statement?
July 31, 2002 at 8:11 am
You'll want to add the IDENTITY property to your column if it is a number column. The catch with IDENTITY columns is that you can add them after the fact, but you can't use a simple ALTER TABLE to remove them.
Here is a link to the ALTER TABLE syntax:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_aa-az_3ied.asp
Basically you are looking to do an
ALTER TABLE MyTable
ALTER COLUMN MyColumn int IDENTITY
You can also modify the column properties through Enterprise Manager.
K. Brian Kelley
http://www.sqlservercentral.com/columnists/bkelley/
K. Brian Kelley
@kbriankelley
August 1, 2002 at 12:46 am
alternatively, if you want to maintain the numbers yourself then this update should work
DECLARE @IncNo
SET @IncNo = 0
UPDATE <Table>
SET @IncNo = <Column> = @IncNo + 1
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply