Introduction
In this blog, I am going to discuss how to rename a table and how to rename column.
Background
Most of the time, if we have to rename a table or rename a column we usually drop table and create table again, but with this approach, we lose data. This approach may work in a development environment but in production, we may not want to lose data. So how to achieve this? In SQL Server, we can achieve it easily.
Rename table name in SQL Server
Here is the syntax to rename a table.
EXEC SP_RENAME 'Old Table Name', 'New Table Name'
Example -
- EXEC SP_RENAME 'UserLogin', 'User'
Rename column name in SQL Server
Here is the syntax to rename column name.
EXEC SP_RENAME 'Table Name.Old column name', 'New Column Name'
Example -
- EXEC SP_RENAME '[User].IsActive1', 'IsActive'