Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Three Ways To Return All Rows That Contain Uppercase Characters OnlyCompound Operators Or How T-SQL Is Morphing Into VB Or C# »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

If you have a table and you want to rename a column without using the designer, how can you do that?

First create this table

  1. CREATE TABLE TestColumnChange(id int)
  2. INSERT TestColumnChange VALUES(1)
  1. SELECT * FROM TestColumnChange

As you can see the select statement returns id as the column name. you can use ALTER table ALTER Column to change the dataype of a column but not the name.

Here is what we will do, execute the statement below

  1. EXEC sp_rename 'TestColumnChange.[id]', 'value', 'COLUMN'

Now do the select, you will see that the column name has changed

  1. SELECT * FROM TestColumnChange

That is it, very simple

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
968 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

2 comments

Comment from: Naomi [Member] Email
Once you renamed the column, how would you automatically fix all references to this column?
07/13/09 @ 07:29
Comment from: SQLDenis [Member] Email
You can't really, you would either need a tool that will fix it for you like DataDude or do this on staging and do some regression testimng there to identify all the objects that refernce this...for all you know you might also have some ad-hoc SQL in apps that will break.....this is also why people in general don't rename stuff even if the column name is misspelled
07/13/09 @ 07:35

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)