In this article i will explain how to rename a column and how to rename a table in SQL Server.
In previous articles i explained How to create a table in SQL Server, How to alter table column in SQL Server, and How to add a column to table and How to delete a column from table in SQL Server. Now i will explain how to rename a column and how to rename a table in SQL Server.
Rename a Column:
Note: There is no command to rename a column in SQL Server. We will use a stored procedure.
Exec sp_rename 'tableName.oldColumnName', 'newColumnName'
Here sp_rename is a procedure. And it will take two parameters. One is table old column name and second is new column name. If you see the first parameter tableName.oldColumnName. tableName and oldColumnName are seperated with dot. tableName, the table which you are dealing with and oldColumnName, the column which you are renaming.
Exec sp_rename 'Employee.EmployeeName', 'EmpName'
Execute the above statement, EmployeeName column will be renamed with EmpName.
Note: After executing above statement it will give one caution as shown below:
This means wherever you are using EmployeeName those scripts and stored procedures will be affected. So, update those scripts and procedures.
Rename a Table:
Exec sp_rename 'oldTableName', 'newTableName'
Here sp_rename will take two parameters. One is table old name and second is table new name.
Exec sp_rename 'Employee', 'Emp1'
Execute the above statement, Employee table will be renamed with Emp1.