SELECT
		' KILL ' + CONVERT(VARCHAR, spid)
	FROM sysprocesses
	WHERE
		DATABASEPROPERTYEX(DB_NAME(dbid), 'Status') = 'ONLINE'
		AND DB_NAME(dbid) NOT IN('master', 'tempdb', 'msdb', 'model')
UNION
SELECT DISTINCT
		'exec sp_detach_db ''' + DB_NAME(dbid) + ''';'
	FROM master.dbo.sysaltfiles
	WHERE
		DATABASEPROPERTYEX(DB_NAME(dbid), 'Status') = 'ONLINE'
		AND DB_NAME(dbid) NOT IN('master', 'tempdb', 'msdb', 'model')
		
