views:

53

answers:

3

how can i rename column name via alter table in Ms sql 2005 forexample:

alter table tablename rename "old col name" to "new col name"
+1  A: 

sp_rename as described here.

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

ho1
i want to make it without sp_rename how can i do that alter....
Phsika
@Phsika: Don't think you can, have frustrated me also sometimes.
ho1
A: 
sp_rename 'TableName.ColumnName', 'NewColumnName', 'COLUMN'
Cambium
A: 

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]

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