-- CREATE PROCEDURE [dbo].[sp_KillAllConnToDatabase](@DatabaseName nvarchar(25))
-- AS
-- BEGIN
	SET NOCOUNT ON;
	-- Variablen deklarieren
	DECLARE @DatabaseName nvarchar(255)
	DECLARE @dbid int, @MinSpid int, @MaxSpid int
	DECLARE @SqlCommand nvarchar(255)
	set @DatabaseName = 'JobManagerV3_CustomerNameShort_WorkInst_TestSystem'
	-- Variablen initialisieren
	SELECT @dbid = [dbid] FROM [master]..sysdatabases WHERE [name] = @DatabaseName;
	SELECT @MinSpid = MIN(spid) FROM [master]..sysprocesses WHERE [dbid] = @dbid;
	SELECT @MaxSpid = MAX(spid) FROM [master]..sysprocesses WHERE [dbid] = @dbid;
	-- While-Schleife zur Schließung der Verbindungen zur Datenbank einsetzen
	WHILE @MinSpid <= @MaxSpid
	BEGIN
		SET @SqlCommand = N'KILL ' + CAST(@MinSpid AS NVARCHAR(5));
		EXEC(@SqlCommand);
		Print ('@SqlCommand' + @SqlCommand);
		SET @MinSpid=@MinSpid+1;
	END
	Print ('Done KILL Connectet Procersses on:' + @DatabaseName);

-- END
GO