超有用。如何将SQL的MODE在MULTI USER和SINGLE USER之间切换

从多用户切换单用户简单,麻烦的是从单用户切换到多用户。可能会总是出现提示有用户连接的情况。

我试过很多其它办法,都遇阻。

以下代码,完美解决。快,准,狠。

推荐。

f you try to access the database which is already in the Single-User mode, you need to close all the connections to the database first, otherwise you will get an error message:

Msg 5064, Level 16, State 1, Line 1   Changes to the state or options of database 'DatabaseName' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.   Msg 5069, Level 16, State 1, Line 1   ALTER DATABASE statement failed.

The following query kills the processes accessing the database:

-- Create the sql to kill the active database connections  
declare @execSql varchar(1000), @databaseName varchar(100)  
-- Set the database name for which to kill the connections  
set @databaseName = 'DatabaseName'  

set @execSql = ''   
select  @execSql = @execSql + 'kill ' + convert(char(10), spid) + ' '  
from    master.dbo.sysprocesses  
where   db_name(dbid) = @databaseName  
     and  
     DBID <> 0  
     and  
     spid <> @@spid  
exec(@execSql)
GO

Then you should be able to bring the database back to Multi-User mode as usual:

ALTERDATABASE'DatabaseName'SET MULTI_USER

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值