pages

Monday, July 14, 2014

How to Rename SQL DB even when it is in USE ?

 

Use this script to rename your db.

This one sets single user mod first so that there is exclusive access, then rename the db and bring back to normal mode (multi_user mode)

 

USE master;
GO

ALTER DATABASE [Old_DB_Name]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

ALTER DATABASE [Old_DB_Name]
MODIFY NAME=
[New_DB_Name]
GO

ALTER DATABASE [New_DB_Name]
SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO