Sqlserver: the database is in single-user mode and cannot be deleted.

Source: Internet
Author: User

When deleting a database today, an error is reported. The error message is: this database is in single-user mode and is still in connection and cannot be deleted (this is also true even when is stopped and then started)

Baidu then found a solution, backed up here:

 
Use [Master] Go/****** object: storedprocedure [DBO]. [killspid] script Date: 03/28/2011 11:01:32 *****/set ansi_nulls ongoset quoted_identifier ongo -- creates a stored procedure and disconnects all users. Create proc [DBO]. [killspid] (@ dbname varchar (20) as begin declare @ SQL nvarchar (500) declare @ spid int set @ SQL = 'descare getspid cursor for select spid from sysprocesses where dbid = db_id (''' + @ 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 go

Create a stored procedure in the master to kill all connections and then call

Use master exec killspid 'problematic database name'

Delete the file and then click OK.

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.