Answer by Murat Ata for Script to kill all connections to a database (More...
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'DatabaseName'GOUSE [master]GOALTER DATABASE [DatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATEGOGOUSE [master]GODROP DATABASE...
View ArticleAnswer by Mohammed Khurram for Script to kill all connections to a database...
If you want to only drop/delete a database you can select the option "Close existing connections" which is by default unset.Right-click on Database catalog -> options.Delete -> check option.Ok
View ArticleAnswer by Mahdi Shahbazi for Script to kill all connections to a database...
USE MASTERGODECLARE @Spid INTDECLARE @ExecSQL VARCHAR(255)DECLARE KillCursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLYFORSELECT DISTINCT SPIDFROM MASTER..SysProcessesWHERE DBID = DB_ID('dbname')OPEN...
View ArticleAnswer by MellowTone for Script to kill all connections to a database (More...
The accepted answer has the drawback that it doesn't take into consideration that a database can be locked by a connection that is executing a query that involves tables in a database other than the...
View ArticleAnswer by Emrah Saglam for Script to kill all connections to a database (More...
SELECT spid, sp.[status], loginame [Login], hostname, blocked BlkBy, sd.name DBName, cmd Command, cpu CPUTime, memusage Memory, physical_io DiskIO, lastwaittype LastWaitType, [program_name]...
View ArticleAnswer by Filip Holub for Script to kill all connections to a database (More...
You can use Cursor like that:USE masterGODECLARE @SQL AS VARCHAR(255)DECLARE @SPID AS SMALLINTDECLARE @Database AS VARCHAR(500)SET @Database = 'AdventureWorks2016CTP3'DECLARE Murderer CURSOR FORSELECT...
View ArticleAnswer by cantoni for Script to kill all connections to a database (More than...
@AlexK wrote a great answer. I just want to add my two cents. The code below is entirely based on @AlexK's answer, the difference is that you can specify the user and a time since the last batch was...
View ArticleAnswer by Shahriar Khazaei for Script to kill all connections to a database...
You should be careful about exceptions during killing processes. So you may use this script:USE master;GO DECLARE @kill varchar(max) = ''; SELECT @kill = @kill +'BEGIN TRY KILL '+ CONVERT(varchar(5),...
View ArticleAnswer by Chris Bates for Script to kill all connections to a database (More...
Matthew's supremely efficient script updated to use the dm_exec_sessions DMV, replacing the deprecated sysprocesses system table:USE [master];GODECLARE @Kill VARCHAR(8000) = '';SELECT @Kill = @Kill...
View ArticleAnswer by Sodoshi for Script to kill all connections to a database (More than...
Little known: the GO sql statement can take an integer for the number of times to repeat previous command.So if you:ALTER DATABASE [DATABASENAME] SET SINGLE_USERGOThen:USE [DATABASENAME]GO 2000This...
View ArticleAnswer by Sacha for Script to kill all connections to a database (More than...
To my experience, using SINGLE_USER helps most of the times, however, one should be careful: I have experienced occasions in which between the time I start the SINGLE_USER command and the time it is...
View ArticleAnswer by Binh Truong for Script to kill all connections to a database (More...
I have tested successfully with simple code belowUSE [master]GOALTER DATABASE [YourDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATEGO
View ArticleAnswer by Pourya for Script to kill all connections to a database (More than...
You can get the script that SSMS provides by doing the following:Right-click on a database in SSMS and choose deleteIn the dialog, check the checkbox for "Close existing connections."Click the Script...
View ArticleAnswer by AlexK for Script to kill all connections to a database (More than...
UpdatedFor MS SQL Server 2012 and aboveUSE [master];DECLARE @kill varchar(8000) = ''; SELECT @kill = @kill +'kill '+ CONVERT(varchar(5), session_id) +';'FROM sys.dm_exec_sessionsWHERE database_id =...
View ArticleAnswer by Chains for Script to kill all connections to a database (More than...
USE masterGOALTER DATABASE database_nameSET OFFLINE WITH ROLLBACK IMMEDIATEGORef: http://msdn.microsoft.com/en-us/library/bb522682%28v=sql.105%29.aspx
View ArticleScript to kill all connections to a database (More than RESTRICTED_USER...
I have a development database that re-deploy frequently from a Visual Studio Database project (via a TFS Auto Build).Sometimes when I run my build I get this error:ALTER DATABASE failed because a lock...
View Article
More Pages to Explore .....