Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Incrementing values - 500,000 records - crazy?

Author  Topic 

nickmollberg
Starting Member

3 Posts

Posted - 2005-07-11 : 16:19:12
I have 500,000 records...
I need to set a 'Company code' A unique identifier for each one...

What would the SQL update syntax be for doing this?

Will this be able to run on that many records?

Thank you in advance for any help you can give.

Tekhne
Starting Member

3 Posts

Posted - 2005-07-11 : 17:39:14
Depends on what kind of id you want. If you truly need a uniqueidentifier, then use:
quote:

Alter Table tablename
Add Column Company_ID uniqueidentifier not null
Update tablename Set Company_ID = NewID()


If you just want a numeric identifier, use:

quote:

Declare @Counter int
Set @Counter = 0
Alter Table tablename
Add Column Company_ID int not null
Update tablename Set @Counter = Company_ID = @Counter + 1




Hope this helps..
Go to Top of Page

nickmollberg
Starting Member

3 Posts

Posted - 2005-07-11 : 20:56:11
the column already exists, and this table is full with data.

I need an UPDATE query that will make the first record to CompanyID = 1, and the last (the 500,000th record to CompanyID = 500,000

So, set the first record to companyID1, then incrememnt each additional record by one.

I guess I would need a loop in my SQL UPDATE to do this?

Any ideas on syntax?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-07-11 : 21:14:22
You can try this:

http://www.sqlteam.com/item.asp?ItemID=765
Go to Top of Page
   

- Advertisement -