关闭指定数据库所有进程连接【适用于2005】

  
    
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER proc [ dbo ] . [ p_killspid ]
@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

ALTER DATABASE [ OJC_ZJ_CN ] SET READ_ONLY WITH NO_WAIT

你可能感兴趣的:(数据库)