SQL Server forces a method to disconnect a database _mssql

Source: Internet
Author: User
When you execute a library script, you tend to drop the original database, and because SQL Server detects a data connection that prevents the drop database operation from being performed, the build script often fails, so we need a way to force a disconnected database. The following T-SQL implementation can be done:
Copy Code code as follows:

DECLARE @i int DECLARE cur cursor for select spid from sysprocesses where db_name (dbid) = ' your_database_name ' Open cur FET CH Next from cur to @i while @ @fetch_status =0 begin EXEC ("Kill" +@i) fetch next from cur to @i end close cur E cur

We can write this SQL into the Library batch script and put it at the beginning of the script:
Copy Code code as follows:

:: Disconnect existing Fortune database connections
Osql-s '%1 '-u '%2 '-p '%3 '-Q ' declare @i int DECLARE cur cursor for select spid from sysprocesses where db_name (dbid) = ' Yo Ur_database_name ' Open cur fetch next from cur to @i while @ @fetch_status =0 begin EXEC (' kill ' +@i) fetch the next from cur Into @i end close cur deallocate cur "
Related Article

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.