Error "Unable to gain exclusive access to the database because the database is in use" solution

Source: Internet
Author: User

Cause: Because when I restore the database, there are other users who are using the database, so the above prompt appears.

Workaround:
1, set the database to work in single-user mode.
Setup method: Right-click on the database you want to restore, select Properties-> Options-> Status-> Restrict Access-> "single" On the right-click menu command. This is the menu command for SQLSERVER2005, other versions please find it yourself.

2, the use of SQL statements to kill all the processes that are using the database, I used to do a SQL Server operation of the small tool has written this function of SQL, posted for your reference:

DECLARE @dbname varchar (50)
Set @dbname = ' database name '
DECLARE @sql varchar (50)
Declare cs_result cursor local for select ' Kill ' +cast (spid as varchar) from sys.sysprocesses where Db_name (dbid) [Emai L protected]
Open Cs_result
FETCH NEXT from Cs_result to @sql
While @ @fetch_status =0
Begin
Execute (@sql)
FETCH NEXT from Cs_result to @sql
End
Close Cs_result
Deallocate Cs_result


The SQL statement uses the cursor to loop through all the processes that are using the database and kills the process with the kill command.

3. Using SQL statements, disconnect all user links and roll back all transactions, with the following SQL statements:

ALTER database [DB name]
SET OFFLINE with ROLLBACK IMMEDIATE



Note: When using method 2 and 3 o'clock, do not execute under the desired restored database, it is recommended to execute under the master database.

Error "The database is in use, so you cannot obtain exclusive access to the database" solution (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.