|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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 |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Auto number an existing field |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|