Everyday, when i have to refresh databases I have to run sp_who2 and see if there is any active connection to database. It's painful process of checking each connection and killing each active process id. After you kill all active connection, you run sp_who2 again and you find few more connection. If you have to kill 3-4 connection, then it's ok to kill one by one but when you have to kill over 10 active connection this process is painful.
After searching online, I have found really good article and would like to share with everyone.
Run Following code after changing your db name
-- Create the sql to kill the active database connections
declare @execSql varchar(1000), @databaseName varchar(100)
-- Set the database name for which to kill the connections
set @databaseName = 'myDatabase'
set @execSql = ''
select @execSql = @execSql + 'kill ' + convert(char(10), spid) + ' '
from master.dbo.sysprocesses
where db_name(dbid) = @databaseName
and
DBID <> 0
and
spid <> @@spid
exec(@execSql)
Here I have found even smaller version of this process.
alter database dbName set single_user with rollback immediate
alter database dbName set multi_user with rollback immediate
I hope it will help many dba's like me. If you have something better to share please post your comment here.
Link: http://blog.tech-cats.com/2008/01/kill-all-database-connections-to-sql.html
No comments:
Post a Comment