--sp_helpdb--查看可用数据库
declare @name sysname, @sql nvarchar(4000)
DECLARE roy CURSOR
FOR
--排除不分离的数据库名就行了
SELECT name FROM sysdatabases where name not in ('master','tempdb','pubs','Northwind','msdb','model')
open roy
FETCH NEXT FROM roy INTO @name
WHILE @@FETCH_STATUS = 0
begin
set @sql=N'
EXEC sp_detach_db '+ @name+', '+ 'true'
EXEC(@sql)
print @name
FETCH NEXT FROM roy INTO @name
end
CLOSE roy
DEALLOCATE roy