you can use following code.
declare @num INT
select @num = 0
UPDATE TableName
SET @num = @num + 1,
datecreated = CONVERT(VARCHAR, @num)
Main Topics
Browse All TopicsI have a user table in my database. It consists of the following fields:
UID (varchar)
datecreated (varchar)
useremail (varchar)
name (varhcar)
address (varchar)
[several other fields that are not important]
for whatever reason I did not use an autonumber at the time I created the table. SQL will not allow it to be converted ( i get error messages). Please do not focus in on that. I'm not looking for a solution involving breaking my index and relationships. I have two other tables that are tied to this one based on the UID field.
I have used a manual index to generate my userID's. This works well for the most part. Another table holds numbers, one to a record. I retrieve the number, increment it and set the next number to be used back in the table.
The catch, and my problem, is that sometimes numbers would get skipped if there was a data writing error. A combination of bad input, poor error control, and some particularly vicious robots that have created 20 and 30 empty accounts at a whack on my particular site.
The end result is that I have data that looks like this:
UID - Name
1 PersonA
5 PersonB
6 PersonC
10 PersonD
What I want to do is renumber my UID column. I propose to use the datecreated column to hold a new ID number. Then I'll update UID=datecreated and then set datecreated back to null. Datecreated was intended for a purpose and has not been implemented so the column is empty.
I have tested this on a single account using a single update statement. It worked fine and didn't break any of the management tools we use to manage users.
Where I am stumped is the code to generate the sequential numbers using SQL.
I've searched and can't find anything to use as an example.
If I were writing it in Basic..... or VBscript it would go:
A hashed up version of what I want to do is this:
for a=1 to numrows(recordset)
set datecreated=a
next
I suppose I could use a cursor.... it's a one time problem to be fixed once. Just wondering if anyone has a better way.
We fixed the data entry problem by adding some controls and implementing CAPTCHA to stop the robots.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: srafi78Posted on 2007-11-01 at 21:18:44ID: 20198407
BEGIN TRAN
Create Table #Temp1(SeqID int identity(1,1) not null, UID int not null)
Insert into #Temp1
Select UID from urTable
Update urTable set UID = t.SeqID
from urTable ut, #Temp1 t
where t.UID = ut.SeqID
select * from urTable -- to check how the updated table looks like
ROLLBACK TRAN -- Transaction used to see that the table was updated correctly before actually doing so.
Is this what you are looking for?