当前位置: 数据库>sqlserver
强制断开sql server数据库已有连接的方法
来源: 互联网 发布时间:2014-08-29
本文导语: 有时需要强制断开sql server数据库已有的连接,可以通过如下的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 ...
有时需要强制断开sql server数据库已有的连接,可以通过如下的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"
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"
另外,分离数据库时,选择切断所有的连接,也是一种办法。