Solved

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

Posted on 2008-10-20
8
1,075 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
  • 4
  • 4
8 Comments
 

Author Comment

by:mechanism
Comment Utility
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
Comment Utility
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
Comment Utility
if not, try remove ID column and run query
ALTER TABLE TABLE_01

ADD ID int identity

Open in new window

0
 

Author Comment

by:mechanism
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mechanism
Comment Utility
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
Comment Utility
you can change to this
ALTER TABLE TABLE_01

ADD NEWID int identity

Open in new window

0
 
LVL 17

Expert Comment

by:HuyBD
Comment Utility
then update
update TABLE_01 set ID=NEWID

Open in new window

0
 

Author Closing Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now