Skip to content

Instantly share code, notes, and snippets.

@benjaminramey
Created February 1, 2012 15:44
Show Gist options
  • Select an option

  • Save benjaminramey/1717627 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminramey/1717627 to your computer and use it in GitHub Desktop.
This script will kill all current connections to a database in MSSQL. This is useful for detaching the database, restoring it or performing other tasks that require all connections dropped from the database.
DECLARE @SQL VARCHAR(8000)
SELECT @SQL=COALESCE(@SQL,'')+'Kill '+CAST(spid AS VARCHAR(10))+ '; '
FROM sys.sysprocesses
WHERE DBID=DB_ID('<DATABASE_NAME>')
EXEC(@SQL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment