You are on page 1of 5

How to rename a SQL Server database

Option 1 - Rename SQL Server Database


using T-SQL
This command works for SQL Server 2005, 2008, 2008R2, 2012,
2014, 2016, 2017 and 2019:

ALTER DATABASE [Test] MODIFY NAME = [Test2]

If you are using SQL Server 2000 you can use the T-SQL command
below to make the database name change. This still works for SQL
2005, 2008, 2008R2, 2012, 2014, 2016, 2017 and 2019, but Microsoft
says it will be phased out at some time.

EXEC sp_renamedb 'Test', 'Test2'

Option 2 - Rename SQL Server Database


using SSMS rename option
If you are using SQL Server Management Studio, right click on the
database and select the Rename option and then rename the
database.
Option 3 - Rename SQL Server Database
using SSMS
Another simple way to rename the database is to just click on the
database name in the Object Explorer and rename the database like
you would rename a folder in Windows.

Option 4 - Rename SQL Server database


using detach and attach
Use the detach and attach feature of SQL Server to detach the
database first and when you reattach the database you give the
database a different name. This can be done by using the following
T-SQL commands (enter your database name and info).

First run the following to get the database file names:

EXEC sp_helpdb 'Test'

First detach the database:

EXEC sp_detach_db 'Test', 'true'

Then attach the database using the files from the first command
above:

EXEC sp_attach_db
@dbname = N'Test2',
@filename1 = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\DATA\
test.mdf',
@filename2 = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\DATA\
test_log.ldf'

Detach and Reattach using SSMS

You can also do this using the SSMS GUI as follows:

We are going to rename database "Test" to "Test2".

Right click on database "Test" and select Tasks > Detach... and
the following will open. Click OK to detach the database.

To reattach, right click on Databases and select Attach...

Then click the Add button and select the MDF file for the database
you want to reattach.

Here we are reattaching database "Test", but we will attach as


"Test2".
One thing to note is by changing the name of the database using
any of these techniques you are only renaming the database. The
physical files still have the same names, so if you want to also
change the name of the files the best approach is to use Option 4.
Before you reattach the files you need to first change the name of
the physical files and then when you do the reattach you can
specify the renamed files.

Important Note - Need Exclusive Database


Access
In order to rename a database you will need to have exclusive
access to the database, which means there are no other database
connections using the database.

I will open another query window and USE database Test2, this way
there is another connection to the Test2 database.

Then when I try to rename database "Test2" back to "Test", it fails


since I did not have exclusive access and I got this error message.

You might also like