SqlServer強迫斷開數據庫已有銜接的辦法。本站提示廣大學習愛好者:(SqlServer強迫斷開數據庫已有銜接的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是SqlServer強迫斷開數據庫已有銜接的辦法正文
在履行建庫劇本時,常常會先將原本的數據庫drop失落,因為SqlServer檢測到稀有據銜接時制止履行drop database操作,所以建庫劇本常常履行掉敗,為此我們須要一種能強迫斷開數據庫已有銜接的辦法,可以過以下t-sql完成:
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"