It's very useful to tackle a locked database, when you decide to restore it.

 

Declare   @tblConnectedUsers   Table   (  
  SPID int )  
   
  Declare   @vcSQLText varchar(200),  
  @iSPID int  
   
  --Get   the   currently   connected   users  
  Insert   into     @tblConnectedUsers  
  Select   p.spid  
  from   master.dbo.sysprocesses   p   (nolock)  
  join   master..sysdatabases   d   (nolock)   on   p.dbid   =   d.dbid  
  Where   d.[name]   =   'dbname'   -->   database   name   here  
   
  --Loop   though   the   connected   users   and   kill   their   connections  
  While   1   =   1  
  Begin  
   
  Select   top   1   @iSPID   =   SPID  
  From     @tblConnectedUsers  
  Where   SPID   >   IsNull(@iSPID,   0)    
  order   by   SPID   asc  
   
  --   break   when   there   are   no   more   SPIDs  
  If   @@RowCount   =   0  
  Break  
   
  --Build   the   SQL   string  
  Set   @vcSQLText   =   'Kill   '   +   Convert(varchar(10),   @iSPID)  
   
  Exec(   @vcSQLText   )  
   
  End  

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2021-07-20
  • 2021-11-29
  • 2022-12-23
  • 2021-11-20
  • 2021-08-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2021-08-18
  • 2022-01-15
  • 2021-12-17
相关资源
相似解决方案