How do you rename a table or column name?

Rename

The stored procedure sp_rename is used to rename a table or a column name in a table.

Syntax:

To rename a table name

——————————————————————————————————————–

sp_RENAME ‘[PresentTableName]’,‘[NewTableName]’

GO

To rename a table column name:

sp_RENAME ‘TableName.[PresentColumnName]’,‘[NewColumnName]’,‘COLUMN’

GO

Ex:

sp_RENAME ‘Mytable’,‘Myrenamedtable’

GO

sp_RENAME ‘Myrenamedtable.Mycolumn’,‘Myrenamedcolumn’,‘COLUMN’

GO

——————————————————————————————————————–

Note:

After the executing any of the above rename commands, you will get the below warning.

Caution: Changing any part of an object name could break scripts and stored procedures.

Renaming an object may cause other objects. Because, some stored procedures or triggers or views might use this table in their code. So if we change the name, all the objects which are depending on it will be failed. So be careful while renaming an object :)

Leave a Reply

Your email address will not be published. Required fields are marked *