Skip to content

Instantly share code, notes, and snippets.

@sarangbk
Last active August 29, 2015 14:08
Show Gist options
  • Save sarangbk/8cc8285855ff0c163460 to your computer and use it in GitHub Desktop.
Save sarangbk/8cc8285855ff0c163460 to your computer and use it in GitHub Desktop.
Rename a Database in MSSQL

Rename a MSSQL Database

Renaming a db fails in MSSQL as it is not in single user mode by default and hence exclusive lock cannot be obtained. Use the following script to put the DB in single user mode -> rename it and then restore it to multiuser mode.

USE master

ALTER DATABASE [Original DB Name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

ALTER DATABASE [Original DB Name] MODIFY NAME = [Renamed DB Name]

ALTER DATABASE [Renamed DB Name] SET MULTI_USER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment