Apr 062011
 

Need to kill all processes connected to a given database, say before a database restore where a web app is trying to reconnect?

 

DECLARE @SQL VARCHAR(8000) 

SELECT @SQL=COALESCE(@SQL,”)+’Kill ‘+CAST(spid AS VARCHAR(10))+ ‘; ‘

FROM sys.sysprocesses

WHERE DBID=DB_ID(‘AdventureWorks’)

EXEC(@SQL)