Удаляет все пользовательские сеансы в базе данных, кроме вашей собственной (что вам запрещено делать)
DECLARE @mySessionId INT
DECLARE @dbName VARCHAR(50)
SET @dbName = '<Your database name>'
DECLARE @spid int
CREATE TABLE #UserSessions (spid SMALLINT, ecid SMALLINT, [Status] NCHAR(30), loginname NCHAR(128), hostname NCHAR(128), blk CHAR(5), dbname NCHAR(128), cmd NCHAR(16), request_id INT)
INSERT INTO #UserSessions ([spid], ecid, [Status], [loginname], [hostname], blk, [dbname], cmd, request_id) EXEC dbo.sp_who
SELECT @spid = min(spid) FROM #UserSessions WHERE NOT hostname = HOST_NAME() AND dbName = @dbName AND [status] = 'sleeping'
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) FROM #UserSessions WHERE NOT hostname = HOST_NAME() AND dbName = @dbName AND spid > @spid AND [status] = 'sleeping'
END
DROP TABLE #UserSessions