Restore the database to see the "no exclusive access" solution (killing the database connection Stored Procedure sqlserver), stored procedure sqlserver

Source: Internet
Author: User

Restore the database to see the "no exclusive access" solution (killing the database connection Stored Procedure sqlserver), stored procedure sqlserver

The stored procedure for creating a master database is as follows:

Createproc killspid (@ dbnamevarchar (20 ))
As
Begin
Declare @ sqlnvarchar (500)
Declare @ spidint
Set @ SQL = 'maid where dbid in (select dbid from sysdatabases where name = '+ @ dbname + ')'
Exec (@ SQL)
Open getspid
Fetch next from getspid into @ spid
While @ fetch_status <>-1
Begin
Exec ('Kill '+ @ spid)
Fetch next from getspid into @ spid
End
Close getspid
Deallocate getspid
End
-- Usage

Use master
Exec killspid '''database name '''

 

Single quotation mark nesting

Use three single quotes. The outermost single quotes indicate vchar. The second single quotes are escaped, and the innermost single quotes are single quotes in the string. declare @ dbname nvarchar (500)

Set @ dbname = '''zcc''' -- the obtained variable is 'zcc', and set @ dbname = 'zcc' -- the obtained variable is zcc.

 


When restoring the database, the system prompts: Because the database is in use, it cannot obtain exclusive access to the database.

It indicates that the service is being used. The service running is disabled. When writing the service, only the editing interface is opened.
 
Use SQL Server2000 to restore the database, prompting that the database is in use and does not have exclusive access to the database. The database cannot be offline.

Try separating the database.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.