展会信息港展会大全

SqlServer强制断开数据库连接的方法
来源:互联网   发布日期:2016-03-01 10:22:31   浏览:3731次  

导读:在执行建库脚本时,往往会先将原有的数据库drop掉,由于SqlServer检测到有数据连接时禁止执行drop database操作,所以建库脚本经常执行失败,为此我们需要一种能强制断开数据库已有...

在执行建库脚本时,往往会先将原有的数据库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"

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港