How to Rename Column Name, or How to rename Table Name is one of the most Frequent Question in SQL Server forums. In this article we will show you, Rename Table Name and Column Name in SQL Server with example.
To demonstrate the same, We are going to use the Employees Details table present in our [SQL Test] Database. And the data present in the Employee Details Table is:
Rename Table Name and Column Name in SQL Server
In this series of examples we are going to use the standard stored procedure SP_RENAME to rename SQL Server Table name and column name.
SQL Server Rename Table Name
In this example we will rename the table name using the SP_RENAME. Syntax behind this approach is as shown below:
-- Syntax for SQL Server rename Table Name is: SP_RENAME '[Old Table Name]', '[New table Name]'
We are going to use above specified syntax to rename the EmployeesDetails table to NewEmployeesDetails
-- SQL Server rename Table Name is: USE [SQLTEST] GO SP_RENAME 'EmployeesDetails', 'NewEmployeDetails'
Now let me try to select records from old table
As you can see from the above screenshot, it is throwing an error. Let me try with New table name
SQL Server Rename Column Name
In this example we will rename column name using the SP_RENAME, syntax behind this approach is as shown below:
-- Syntax for SQL Server rename Column Name is: SP_RENAME '[Table Name].[Old Column Name]', '[New Column Name]', 'COLUMN'
We are going to use above specified syntax to rename the old column name YearlyIncome to new name: Annual Income
-- SQL Server Rename Column Name query is: USE [SQLTEST] GO SP_RENAME 'NewEmployeDetails.YearlyIncome', 'Annual Income', 'COLUMN'
From the below screenshot you can see the changed column
Thank You for Visiting Our Blog