10

How can I rename a column via alter table in MS SQL 2005?

For example:

alter table tablename rename "old col name" to "new col name"

5 Answers 5

21
sp_rename 'TableName.ColumnName', 'NewColumnName', 'COLUMN'
Sign up to request clarification or add additional context in comments.

Comments

4

i want to make it without sp_rename how can i do that alter..

You can't. You can create a new column in the table, using the new name, copy the contents of the old column into the new column, and then drop the old column (that's two ALTERs and an UPDATE), but the only way to do it otherwise is sp_rename.

Here's a link to the ALTER TABLE documentation, where you can see what options are available to you. Change isn't one of them.

This section of the documentation covers what you can do as part of an ALTER COLUMN clause of ALTER TABLE:

ALTER COLUMN column_name 
{ 
    [ type_schema_name. ] type_name [ ( { precision [ , scale ] 
        | max | xml_schema_collection } ) ] 
    [ COLLATE collation_name ] 
    [ NULL | NOT NULL ] 
| {ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION}
}

Note, there's no mention of a new name. So, again, to repeat, you can't rename a column using ALTER TABLE, in SQL Server. If they implemented the standard syntax (which they don't), it would be ALTER TABLE [table_name] RENAME {COLUMN} [column_name] TO [new_column_name]

1 Comment

Alter table 'Customer' change 'newColumnName' '[First_Name]' type size is it true CHANGE is not in ms sql?
2

sp_rename as described here.

Though I seem to remember that it can't always be used.

1 Comment

@Phsika: Don't think you can, have frustrated me also sometimes.
1

Try this to rename the column is

EXEC sp_RENAME 'table_name.old_Column_name', 'new_Column_name', 'COLUMN'

Comments

0

For MySQL the syntax is:

ALTER TABLE <db>.<table> CHANGE COLUMN <old_column_name> <new_column_name> <column_type>;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.