批量分离SQL数据库语句

--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

 

posted @ 2014-04-01 10:31  飞花雪月  阅读(1645)  评论(0编辑  收藏  举报