Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Script Needed (Modifying Records)

Posted on 2007-10-05
4
Medium Priority
?
229 Views
Last Modified: 2010-03-20
Hi all,

I need an SQL script that will do the following:

Under a Column called ID I have a list of numbers.

I need to change those number from for example:

ID
---
34
24
54
65
3
5
21

TO:

ID:
---
1
2
3
4
5
6
7

Till the end BUT without changing the rest of the records.

So it would be something like this:

From:

Hi all,

I need an SQL script that will do the following:

Under a Column called ID I have a list of numbers.

I need to change those number from for example:

ID      Description
---------------------
34     A Red Car
24     A Blue Car
54     A Green Car
65    etc......
3
5
21

To This:


ID      Description
---------------------
1     A Red Car
2     A Blue Car
3     A Green Car
4    etc......
5
6
7

If possible, how could I do this?

Thanks

ST3VO


0
Comment
Question by:ST3VO
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20020897
Create another column with identity  (ALTER Table TableName ADD newColumn INT IDENTITY   )
then you can drop the original column and later rename the above column to ID
0
 

Author Comment

by:ST3VO
ID: 20020909
Could I have an example code please? I'm not very good with SQL :o/

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 20020924
ALTER Table TableName ADD newColumn INT IDENTITY
GO
---now check whether the data is in the proper order by running a select statement
ALTER TABLE TableName
DROP COLUMN  ID
GO

--Rename the existing column
EXEC sp_rename 'UrTableName.[NewColumn]', 'ID', 'COLUMN'
0
 

Author Comment

by:ST3VO
ID: 20021028
Great!!! Thanks a lot!!!

ST3VO
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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