SQL Server: How to rename a column and How to rename a table

 SQL Server     sbo     Updated on Sep 21, 2013     VIKRAM     935   

Introduction:

In this article i will explain how to rename a column and how to rename a table in SQL Server.

Description:

In previous articles i explained How to create a table in SQL ServerHow 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.

Syntax:

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.oldColumnNametableName and oldColumnName are seperated with dottableName, the table which you are dealing with and oldColumnName, the column which you are renaming.

Example:

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:

Syntax:

Exec sp_rename 'oldTableName', 'newTableName'

Here sp_rename will take two parameters. One is table old name and second is table new name.

Example:

Exec sp_rename 'Employee', 'Emp1'

Execute the above statement, Employee table will be renamed with Emp1.

  Related Articles
Java Script: Create/Insert rows dynamically to HTML Table and Remove/Delete rows from HTML Table
Java Script: Create HTML Table dynamically
SQL Server: Delete record from parent table when there is Foreign Key relation between parent and child tables
SQL Server: NOT NULL Constraint
SQL Server: Temporary Table
SQL Server: How to add a column to table and How to delete a column from table
ASP.Net: ListView Basics - ItemDeleting
SQL Server: How to create a table
  Recent Articles
Java Script: Create/Insert rows dynamically to HTML Table and Remove/Delete rows from HTML Table
Java Script: Create HTML Table dynamically
SQL Server: Delete record from parent table when there is Foreign Key relation between parent and child tables
SQL Server: NOT NULL Constraint
SQL Server: Temporary Table
SQL Server: How to add a column to table and How to delete a column from table
ASP.Net: ListView Basics - ItemDeleting
SQL Server: How to create a table