Search This Blog

Thursday 10 May 2018

Renaming Database Name using Query in SQL Server

Sometimes if we try to rename the database in our development or any other environment we got the error message that this database is in use by other process and we cant rename it.

So first we need to check if any user is not using that database and try to rename it. If still we got the same error then might be some services using that database.

Instead of investing time to investigate which service is causing the issue to rename the database you can simply write the below query to rename your database.

ALTER DATABASE oldatabasename SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

ALTER DATABASE olddatabasename MODIFY NAME = newdatabasename

ALTER DATABASE newdatabasename SET MULTI_USER WITH ROLLBACK IMMEDIATE;


Thanks,
Zahid