微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SqlServer: 单用户模式下查杀相关进程实现单/多用户转换 .

sqlServer下,单/多用户模式转换的三种方法

  1.直接在Management Studio中设置相关数据库的Properties:Options-->Restrict Access下设置MULTI/SINGLE_USER。

   2.若是其他人设置Server为单用户模式,或者其他未知进程占用操作DB,再使用第一种方法发现该DB的Properties是打不开的,这时应该使用如下T-sql进行操作:

?
-----------------------Conversion Between Single Modle and  Multiple Modle,the first method:
EXEC    sp_dboption   @dbname   =   'DB Name' ,   @optname   =   'single   user ' sql plain" style="position:static!important; padding-bottom:0px!important; line-height:2em!important; border-right-width:0px!important; overflow-x:visible!important; overflow-y:visible!important; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:'Courier New',   @optvalue   =   false
'DB Name ' true

 


  3.也可以直接更改DB模式:

?
sql comments" style="position:static!important; padding-bottom:0px!important; line-height:2em!important; border-right-width:0px!important; overflow-x:visible!important; overflow-y:visible!important; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-left:0px!important; outline-width:0px!important; width:auto!important; bottom:auto!important; padding-right:0px!important; font-family:'Courier New',the second method:
ALTER  DATABASE  DealManager SET  MULTI_USER
AlTER  DEALMANAGER SINGLE_USER


  倘若在单用户模式下恢复多用户模式或者操作DB发现操作总是失败,这是因为有其他进程占用该DB的缘故,有人会直接手动查看监视器中的进程,把与目的库有关的KILL掉,这种方法是很粗糙的,效率低而且有操作错误进程的危险。其实,实时操作DB的进程在master中是有记录的,我们只需要一个select语句就知道该进程,  然后Kill相关进程:

[sql] view plaincopyprint?
  1. ----------------------Kill the process in Single Modle   
  2. select * from master..sysprocesses  
  3. where dbid=db_id('DB Name')  
  4. --Kill spid  

也写了个小脚本执行这个操作:

DECLARE @kid varchar(100)  
  • SET @kid=''  
  • SELECT @kid=@kid+'KILL'+CAST(spid as Varchar(10))  
  • FROM master..sysprocesses  
  • WHERE dbid=DB_ID('DB Name')  
  • PRINT @kid  
  • EXEC(@kid)  
  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐