SQL杀死活动线程

客户的数据库需要搬迁,但是当前数据库还有其它业务查询,不能断开,在搬迁的过程中,客户端不允许写入数据。

所以决定把客户端连接所使用的用户连接线程杀死,再备份后转移。

 
  
1 declare @dbname varchar ( 20 ) -- -杀死的连接数据库名
2  
3   set @dbname = ' tempdb ' -- -设置数据库名
4  
5 declare @sql nvarchar ( 500 )
6 declare @spid int -- SPID 值是当用户进行连接时指派给该连接的一个唯一的整数
7 set @sql = ' declare getspid cursor for select spid from sysprocesses where
  dbid=db_id( ''' + @dbname + ''' ) and loginame= '' loginuser '''
8 -- sysprocesses 包含有关SQL Server进程的信息。
9 exec ( @sql )
10 open getspid
11
12 fetch next from getspid into @spid
13
14 while @@fetch_status <> - 1 -- 如果FETCH语句没有执行失败或此行不在结果集中。
15 begin
16 exec ( ' kill ' + @spid ) -- 终止正常连接
17 fetch next from getspid into @spid
18 end
19
20 close getspid
21 deallocate getspid
22
23
24 -- 查看活动的线程
25 -- select login_time, last_batch, hostname, loginame, * from sysprocesses
  --where db_id('''+@dbname+''') and loginame ='loginuser'

转载于:https://www.cnblogs.com/371448009/archive/2011/05/20/2051752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值