SqlServer強(qiáng)制斷開數(shù)據(jù)庫已有連接的方法
更新時間:2012年12月19日 16:32:30 作者:
在執(zhí)行建庫腳本時,往往會先將原有的數(shù)據(jù)庫drop掉,由于SqlServer檢測到有數(shù)據(jù)連接時禁止執(zhí)行drop database操作,所以建庫腳本經(jīng)常執(zhí)行失敗,為此我們需要一種能強(qiáng)制斷開數(shù)據(jù)庫已有連接的方法,需要的朋友可以參考下
在執(zhí)行建庫腳本時,往往會先將原有的數(shù)據(jù)庫drop掉,由于SqlServer檢測到有數(shù)據(jù)連接時禁止執(zhí)行drop database操作,所以建庫腳本經(jīng)常執(zhí)行失敗,為此我們需要一種能強(qiáng)制斷開數(shù)據(jù)庫已有連接的方法,可以過如下t-sql實(shí)現(xiàn):
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
我們可以把這條sql寫到建庫的批處理腳本里,放在腳本的開始:
:: 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"
復(fù)制代碼 代碼如下:
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
我們可以把這條sql寫到建庫的批處理腳本里,放在腳本的開始:
復(fù)制代碼 代碼如下:
:: 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"
相關(guān)文章
SQL Server數(shù)據(jù)庫遷移到MySQL的完整指南
在企業(yè)應(yīng)用開發(fā)中,數(shù)據(jù)庫遷移是一個常見的需求,隨著業(yè)務(wù)的發(fā)展,企業(yè)可能會從 SQL Server 轉(zhuǎn)向 MySQL ,原因可能是成本、性能、跨平臺兼容性等,本文將詳細(xì)介紹如何將 SQL Server 數(shù)據(jù)庫遷移到 MySQL,并提供一些實(shí)用的技巧和注意事項(xiàng),需要的朋友可以參考下2025-02-02
SQL Server中row_number函數(shù)的常見用法示例詳解
這篇文章主要給大家介紹了關(guān)于SQL Server中row_number函數(shù)的常見用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
在程序中壓縮sql server2000的數(shù)據(jù)庫備份文件的代碼
在程序中壓縮sql server2000的數(shù)據(jù)庫備份文件的代碼...2007-03-03
SQL Server2022安裝教程的實(shí)現(xiàn)步驟(圖文教程)
在日常的工作中,sql server作為一款常用的數(shù)據(jù)庫管理系統(tǒng),安裝與配置就顯得非常重要,本文主要介紹了SQL Server2022安裝教程的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-09-09
簡析SQL Server數(shù)據(jù)庫用視圖來處理復(fù)雜的數(shù)據(jù)查詢關(guān)系
本文我們主要介紹了SQL Server數(shù)據(jù)庫用視圖來處理復(fù)雜的數(shù)據(jù)查詢關(guān)系的相關(guān)知識,以及視圖的優(yōu)缺點(diǎn)和創(chuàng)建方式以及注意事項(xiàng)的相關(guān)知識,需要的朋友可以參考下2015-08-08

