Get the App
SLTechnology News&Howtos  ›  Database  › 

How to forcibly disconnect existing database connections in SqlServer

Shulou Source: shulou.com Published: 2022-05-31 19:26:05 09月21日 Update

This article shows you how to forcibly disconnect the existing database in SqlServer. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

When executing the database building script, the original database drop is often dropped first. Because SqlServer forbids the drop database operation when it detects a data connection, the database building script often fails, so we need a method that can force the database to be disconnected. You can do the following t-sql implementation: copy the code as follows: declare @ i int declare cur cursor for select spid from sysprocesses where db_name (dbid) = 'Your_Database_Name' open cur fetch next from cur into @ I while @ @ fetch_status=0 begin exec (' kill'+ @ I) fetch next from cur into @ i end close cur deallocate cur

We can write this sql into the batch script that builds the library. At the beginning of the script: copy the 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) = 'Your_Database_Name' open cur fetch next from cur into @ I while @ @ fetch_status=0 begin exec ('kill' + @ I) fetch next from cur into @ i end close cur deallocate cur"

The above is how to forcibly disconnect the database in SqlServer. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Data database code script compulsion content skills knowledge concise to the point that is articles methods more articles industries information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple OPPO Reno Shulou Tech Info Xiaomi