How to Rename a Database

If you ever need to rename a database, I recommend using the following script. It ensures no other users are connected. This is important, otherwise your connection won’t be able to gain a lock.

USE master
GO
ALTER DATABASE OriginalDatabase 
SET SINGLE_USER 
WITH ROLLBACK IMMEDIATE
GO
EXEC master..sp_renamedb 'OriginalDatabase ','NewDatabase'
GO
ALTER DATABASE NewDatabase
SET MULTI_USER 
GO

I hope that helps!

Subscribe

0 comments