SQL – Get Exclusive Lock – Fix for The database could not be exclusively locked to perform the operation error

In order to run certain update commands against an SQL database you need to gain exclusive lock. This is done by setting the database to single user mode.
The command is:

ALTER DATABASE [DATABASE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

To set the database back to multi user mode you must remember to run the following code once you’ve completed the tasks requiring single user mode:

ALTER DATABASE [DATABASE] SET MULTI_USER

The following code shows how to alter the collation of a database which requires single user mode.

ALTER DATABASE [DATABASE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE [DATABASE] COLLATE Latin1_General_CI_AS
ALTER DATABASE [DATABASE] SET MULTI_USER

 

Advertisements

One thought on “SQL – Get Exclusive Lock – Fix for The database could not be exclusively locked to perform the operation error

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s