Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old December 13th, 2004, 11:59 AM
stretcher25 stretcher25 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 19 stretcher25 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 50 m 55 sec
Reputation Power: 0
Auto number an existing field

I have a table with a int auto inc field. I updated the table with over 1000 records. I created a field called LeadNumber. I want to write a script to run in query analyzer that will start at the number 1 for the first record and increment by 1 until all records are updated. So lets say I have 1,245 records. The last record will hav the number 1,245 in the field LeadNumber and every field will be updated with a incremeting number.

Reply With Quote
  #2  
Old December 13th, 2004, 12:45 PM
Lauramc's Avatar
Lauramc Lauramc is offline
SQL Slarentice
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: In My Happy Place
Posts: 1,783 Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 15 h 48 m 56 sec
Reputation Power: 1095
If your field auto increments, it should already have the correct values, but assuming that you need to copy the value to a new field, you would need a Cursor. For Example (assumes that your LeadNumber column would be the same as the ID column):

DECLARE @NewID int

DECLARE AssignValue CURSOR FORWARD_ONLY FOR

SELECT
id
FROM
sometable
OPEN AssignValue

FETCH NEXT FROM AssignValue INTO @NewID

WHILE @@FETCH_STATUS = 0

BEGIN

UPDATE
sometable
SET
[LeadNumber] = (@NewID WHERE CURRENT OF AssignValue)


END

FETCH NEXT FROM AssignValue INTO @NewID

END
CLOSE AssignValue

DEALLOCATE AssignValue

Does this help, or am I misunderstanding your dilema?

If you need to ADD an ID column at run time instead of copying it, you might do this:
ALTERTABLE sometable ADD[id] int IDENTITY(1,1)NOTNULLPRIMARY KEY

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Auto number an existing field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway