Solved

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

Posted on 2008-10-20
8
1,127 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

624 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