Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to forcibly disconnect existing database connections in SqlServer

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report