您现在的位置是:网站首页> 编程资料编程资料
SqlServer强制断开数据库已有连接的方法_MsSql_
2023-05-26
424人已围观
简介 SqlServer强制断开数据库已有连接的方法_MsSql_
在执行建库脚本时,往往会先将原有的数据库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"
复制代码 代码如下:
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"
相关内容
- SQL语句检测sp4补丁是否安装_MsSql_
- SQL数据库日志已满解决方法_MsSql_
- sql 2000清空后让表的id从1开始等数据库操作_MsSql_
- SQL order by ID desc/asc加一个排序的字段解决查询慢问题_MsSql_
- 使用xp_cmdshell注销Windows登录用户(终端服务器超出最大连接数)_MsSql_
- sql server字符串非空判断实现方法_MsSql_
- sqlldr装载数据实现代码_MsSql_
- Sql Server触发器的使用_MsSql_
- jdbc连接sql server数据库问题分析_MsSql_
- SQL Server高可用的常见问题分析_MsSql_
