Rename Column Name in SQL Server

In SQL Server, I have a table “tblRename” in which there are the two columns Id and Name.

result

But by mistake I misspelled Name to Names and now I want to change the column name to Name.

So, how is it possible to rename the column name without dropping the table?

We can use a System Stored Procedure sp_rename.

  1. EXEC sp_rename 'tblRename.Names''Name','column' 

sp_rename is the Stored Procedure.

The first value is the tblRename that is the table in which the column is to be renamed, as in "Names" -> tblRename.Names.

The second value is the new column name that we want to change it to, in other words Name.

The third value is the type and here the type is column.

Execute the preceding query.

Execute

So, the column name is now changed to Name.

But when we execute our query we get a caution message.

message

So, if there are any Stored Procedures and scripts pointing to this tblRename table in which you have specified the “Names” column then that Stored Procedure or script will not come in use anymore because now there is no column present with a name “Names”. So, if you want to change the column name then do it before creating any important Stored Procedures or scripts.

Thank you.


Similar Articles