Error renaming database in SQL Server 2008

I right clicked on a database in SQL Server Management Studio and selected rename. Gave it a new name and hit enter. Eventually this error comes back:

Msg 5030, Level 16, State 2, Line 1

The database could not be exclusively locked to perform the operation.

The better way to rename the database is using SQL running these queries:

use master;
ALTER DATABASE [database.name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE [database.name] MODIFY NAME = [database.name.old];
ALTER DATABASE [database.name.old] SET MULTI_USER;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s