--kill所有会话
use master
go
declare @spid varchar(20),@dbname varchar(20)
declare #spid cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #spid
fetch next from #spid into @spid
while @@fetch_status=0
begin 
exec('kill '+@spid)
fetch next from #spid into @spid
end 
close #spid
deallocate #spid--分离数据库
use master
go
EXEC sp_detach_db @dbname