Renaming a SQL Server database

From Dot2DotCommunications
Jump to: navigation, search

The following T-SQL script will rename a database:

use master
go 

alter database [original]
modify name = [renamed]
go

If someone's using the database, you'll get a message like this:

Msg 5030, Level 16, State 2, Line 4
The database could not be exclusively locked to perform the operation.

If the operation is successful, you'll get something like this:

The database name 'renamed' has been set.

It's a good idea to back up the master database after renaming databases.