Solved

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

Posted on 2008-10-20
8
1,123 Views
Last Modified: 2011-10-19
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.
0
Comment
Question by:mechanism
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 

Author Comment

by:mechanism
ID: 22756027
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..
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22756042
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

0
 
LVL 17

Expert Comment

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

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:mechanism
ID: 22756125
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
0
 

Author Comment

by:mechanism
ID: 22756130
Names of fields are not important, I just want to add auto increment in column 'ID'
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 125 total points
ID: 22756195
you can change to this
ALTER TABLE TABLE_01
ADD NEWID int identity

Open in new window

0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22756196
then update
update TABLE_01 set ID=NEWID

Open in new window

0
 

Author Closing Comment

by:mechanism
ID: 31507755
Thank you for your solution, was exactly what I was looking for.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question