Recently, a new MNC took over my current organization. My manager asked me to rename our database’s existing tables based on the latest company name. As a developer, I analyzed the requirements and identified the best approaches to changing the table names in the SQL server.
Approach-1 Using the sp_rename Stored Procedure
You can rename the table using the sp_rename stored procedure.
Syntax
EXEC sp_rename 'old table name', 'new table name'
Example
We can execute the below query to rename from the Orders table to OrderLatest.
EXEC sp_rename 'Orders', 'OrderLatest'
The table name has been changed successfully, as shown in the screenshot below.
Check out How to get table size in SQL Server
If you are required to rename multiple tables at once, you can run the above query multiple times, as shown below.
EXEC sp_rename 'Orders', 'OrderLatest'
EXEC sp_rename 'Products', 'ProductsLatest'
EXEC sp_rename 'Vehicle', 'VehicleLatest'
Approach-2 Using the SQL Server Management Studio
Follow the below steps.
1. On the object explorer, right-click the table name and select the Rename option, as shown in the screenshot.
2. Enter the new name and press the Enter key from your keyboard.
The name changed successfully, as shown in the screenshot below.
Check out How To Check If Table Exists In SQL Server
Or,
You can do this by clicking on the table name, entering the new table name, and pressing the enter key, as shown below.
Conclusion
Renaming tables in SQL Server looks quite easy, but you need to be a little careful while renaming the table as it may break the existing functionality.
You may also like following the articles below.
- How To Update Statistics On A Table In SQL Server
- How to create index on temp table in SQL Server
- How To Enable CDC On A Table In SQL Server
- How To Create A Table Valued Function In SQL Server
Grey is a highly experienced and certified database expert with over 15 years of hands-on experience in designing, implementing, and managing complex database systems. Currently employed at WEX, USA, Grey has established a reputation as a go-to resource for all things related to database management, particularly in Microsoft SQL Server and Oracle environments. He is a Certified Microsoft SQL Server Professional (MCSE: Data Management and Analytics) and Oracle Certified Professional (OCP), with Extensive database performance tuning and optimization knowledge and a proven track record in designing and implementing high-availability database solutions.