大家在还原Sqlserver 2005数据库的时候,有没有碰到"因为数据库正在使用,所以无法获得对数据库的独占访问权"的问题?今天在公司我正好碰到了这问题,中间把数据库重启,想瞬间断开别人访问,瞬间还原。以前数据量少的时候还可以,但现在数据量太大1.5G的数据,导致还原时间长,别人还会连接着而无法法进行还原,那怎么办呢?后来在网上查了下资料,找到了个断开的sql语句,还真管用,在查询分析器里执行就可以了,和大家分享下:

  use master

  go

  if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[P_KillConnections]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

  drop procedure [dbo].[P_KillConnections]

  GO

  create proc P_KillConnections

  @dbname varchar(200)

  as

  declare @sql nvarchar(500)

  declare @spid nvarchar(20)

  declare #tb cursor for

  select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)

  open #tb

  fetch next from #tb into @spid

  while @@fetch_status=0

  begin

  exec('kill '+@spid)

  fetch next from #tb into @spid

  end

  close #tb

  deallocate #tb

  go