[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do you change a column name of a  table in an SQL Server 2005 database?

Posted on 2008-06-17
5
Medium Priority
?
691 Views
Last Modified: 2008-06-28
I just successfully migrated the backend of an Access 2000 database into SQL Server 2005. Some of the column names in a couple of the tables were altered in the migration process and need to be changed. How do you alter a column name of a Table in a SQL Server 2005 database?  Thanks,
0
Comment
Question by:PDSWSS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
J_Carter earned 1000 total points
ID: 21808318
This example renames the contact title column in the customers table to title.

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'
0
 
LVL 8

Assisted Solution

by:Yiogi
Yiogi earned 1000 total points
ID: 21808400
Either from the sql server management studio, or just write the sql code to do it. Either as J Carter said or with the plain good old way:
ALTER TABLE TableName CHANGE PreviousColumnName NewColumnName VarChar(50)

Note that VarChar(50) is just your datatype. You could put whatever you need there.

Should you need to simply change the datatype you could do:
ALTER TABLE TableName MODIFY ColumnName VarChar(20)
0
 

Author Comment

by:PDSWSS
ID: 21808518
Thanks for your quick responses -

It appears that SQL renamed 6 consecutive columns with the same name. However, the data appears to be in the correct order if the columns had their correct names.
According to your instructions I would need to rename 5 of these 6 columns with a different name before
I could change them each to the correct name since this command couldn't distinguish between 6 columns with the same name.  Is there a better way to do this?  Thanks,
0
 
LVL 8

Expert Comment

by:Yiogi
ID: 21811587
Are you sure they have the exact same name? I don't think that's allowed and most likely it wouldn't let you rename in such a case. It will never let you create a table with columns that have the same name. can you script the table as create and post it here?

Thanks.
0
 

Author Comment

by:PDSWSS
ID: 21816535
After looking more closely I see I was mistaken - they are very similar but not exactly the same.
As soon as I get a chance I will try your alter column title suggestions.

Thanks,
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
What we learned in Webroot's webinar on multi-vector protection.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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