DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses
--WHERE dbid = db_id('target_userdatabase_name')
--and spid<>mn
print @kill
EXEC(@kill);
note and example:
1. if you want to know who has connections to a specific user database, you can include where clause WHERE dbid = db_id('target_userdatabase_name')
2. be careful to exclude your own connection eg and spid<>mn
3. sample:
otherwise when you run EXEC(@kill), you'll disconnect yourself from database server.
DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses
--WHERE dbid = db_id('AdventureWorks2012')
--and spid<>55
print @kill
EXEC(@kill);