An error occurs because the database is in use, so it cannot obtain the exclusive access to the database.

Source: Internet
Author: User
When restoring the database today, the system prompts that the database is in use, so it cannot obtain exclusive access to the database. No matter whether I restart the database or restart the computer, the problem cannot be solved, after several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it. Trigger original

Today, when restoring the database, the system prompts "because the database is in use, it cannot obtain exclusive access to the database". No matter whether I restart the database or restart the computer, the problem cannot be solved, after several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it. Trigger original

Today, when restoring the database, the system prompts "because the database is in use and the Hong Kong virtual host cannot obtain exclusive access to the database", whether I restart the database or the computer, server leasing in Hong Kong cannot solve the problem. After several attempts, the problem was finally solved. The cause of the problem and the solution are written. If there is anything wrong, you are welcome to raise it.

Cause: When I restore the database, other users are using the database, so the above prompt will appear.

There are three solutions:

First, use the cursor to loop through all processes that are using the database and kill the process.

Declare @ dbname varchar (50)
Set @ dbname = 'database name'
Declare @ SQL varchar (50)
Declare cs_result cursor local for select 'Kill '+ cast (spid as varchar (50) from sys. sysprocesses where db_name (dbid) = @ dbname
Open cs_result
Fetch next from cs_result into @ SQL
While @ fetch_status = 0
Begin
Execute (@ SQL)
Fetch next from cs_result into @ SQL
End
Close cs_result
Deallocate cs_result

Note: Do not run the command in the database to be restored. We recommend that you run the command in the master database.

Second, use SQL statements to disconnect all users and roll back all transactions.

Alter database [DATABASE name]
SET OFFLINE WITH ROLLBACK IMMEDIATE

Note: Do not run the command in the database to be restored. We recommend that you run the command in the master database.

Third: Set the database to work in single-user mode

Right-click the database to be restored, and select Hong Kong Space, right-click the menu and choose "properties"> "option"> "status"> "Restrict Access"> "Single ". This is the menu command of SQLSERVER2005. For other versions, find them by yourself.

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.