Link to home
Start Free TrialLog in
Avatar of N M
N MFlag for Luxembourg

asked on

Adding a serial number column in a table (SQL Server 2000)

I have a table in SQL Server 2000, named TABLE_01.
I have a field named ID.
I would like to update it with a serial number starting from 1 (1, 2, 3, 4, ... etc) for all records.
How shoud I do that?

ROW_NUMBER() function does not work.
Avatar of N M
N M
Flag of Luxembourg image

ASKER

Example:

I have TABLE_01
   ID        FIELD1     FIELD2
---------------------------------
<NULL>   ABC         DEF
<NULL>   SJH          KLH
etc..

and I want ..

   ID        FIELD1     FIELD2 etc..
---------------------------------
     1        ABC         DEF
     2        SJH          KLH
etc..
if ID is auto-increment you can query to add number column
select TABLE_01.*,
(slect count(*)+1 from TABLE_01 as t where t.ID<TABLE_01.ID) as num
from TABLE_01
order by ID

Open in new window

if not, try remove ID column and run query
ALTER TABLE TABLE_01
ADD ID int identity

Open in new window

Avatar of N M

ASKER

It doesn't seem to work although I did understand the logic behin it..
It actually brings a new column (or, as you correctly put it, the ID column if I delete it beforehand)
but with '1' only..

2008-10-20-114117.jpg
Avatar of N M

ASKER

Names of fields are not important, I just want to add auto increment in column 'ID'
ASKER CERTIFIED SOLUTION
Avatar of HuyBD
HuyBD
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
then update
update TABLE_01 set ID=NEWID

Open in new window

Avatar of N M

ASKER

Thank you for your solution, was exactly what I was looking for.