SQL Server RENAME TABLE

Learning Objective

The objective of this tutorial is to teach you how to rename an existing table in SQL Server database using Transact SQL and using SQL Server Management Studio(SSMS).

SQL Server RENAME TABLE Introduction

Sometime you may need to change an existing table name in the Server database. SQL Server does not provide any ALTER TABLE statement to rename any table name. But it does provide a stored procedure by which you can modify a table name. Alternatively, you can the SQL Server Management Studio to rename a table.

1) RENAME TABLE using Transact SQL

As mentioned above, SQL Server does not provide any direct ALTER statement to rename a table. However, it does provide a store procedure named sp_rename to modify a table name.

SQL Server RENAME TABLE Syntax

The following shows the syntax of RENAME TABLE in SQL Server.

EXEC sp_rename 'old_table_name', 'new_table_name';

Note that, both old table name and new table name must be enclosed with single quotes.

SQL Server RENAME TABLE Example

First, create a table named emp in company database. By default the table will be created in the dbo schema under company database.

CREATE DATABASE company;

USE company;

CREATE TABLE emp
(
emp_id INT PRIMARY KEY IDENTITY (1,1),
first_name nVARCHAR(50) NOT NULL,
last_name nVARCHAR(50) NOT NULL,
dob DATE NOT NULL,
join_date DATE NOT NULL,
phone nVARCHAR(20),
dept_id INT NOT NULL
);
SQL Server rename table example

Now, if you want to change the name of the table from emp to employee, you need to use sp_rename store procedure to rename the table as follows.

EXEC sp_rename 'emp', 'employee';

SQL Server shows the below message.

Caution: Changing any part of an object name could break scripts and stored procedures.

However, it renamed the table successfully as shown in the below image.

2) RENAME TABLE using SSMS

The second method is for renaming table in SQL Server is by using SQL Server Management Studio.

In this example, we change the name of the employee table to emp again.

For this, first right click on the table name and select Rename option.

SQL Server rename table

Second, give the desire name you want to keep and press enter.

SQL Server rename table example

Now you can see that the table name has been rename to emp from employee.

Summary

In this tutorial, you have learned how to rename an existing table in SQL Server database using both Transact SQL and using SQL Server Management Studio.

Advertisement