|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 01, 2008 8:15 PM
Points: 1,
Visits: 2
|
|
All I'm trying to detach multiple databases with the sp_detach_db. All the databases (about 20 of them) have a similar name like LM_04302008. I was hoping to detach all of them in one shot by using a wild card (%) but its not working. How can I detach 20 databases in one shot with one query? I'm a rookie at this and have not found anything on line to help. Any help is appreciated.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:55 PM
Points: 2,471,
Visits: 2,066
|
|
You could try something like the following.
USE MASTER declare databases cursor for select name from sysdatabases where name not in ('master', 'tempdb', 'msdb', 'model', ...any other databases you do not want detached) declare @db sysname declase @sql varchar (5000) open databases
fetch next from databases into @db while @@fetch_status = 0 begin -- not 100% sure of the detach db syntax 'coz I do not have access to SQL where I am - it might be incorrect set @sql = 'sp_detach_db ' + @db exec (@sql) fetch next from databases into @db
end close databases deallocate databases
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 2:45 AM
Points: 416,
Visits: 521
|
|
You can use the below script to do the same. I've excluded system db's since the same can't be removed.
set nocount on declare @dbname as varchar(80) declare @server_name as varchar(20) select @server_name = @@servername declare rs_cursor CURSOR for select name from master.dbo.sysdatabases where name not in ('model','master','msdb','tempdb','alert_db','mssecurity') open rs_cursor Fetch next from rs_cursor into @dbname IF @@FETCH_STATUS <> 0 PRINT 'No database to backup...Please check your script!!!' WHILE @@FETCH_STATUS = 0 BEGIN print 'sp_detach_db ' + @dbname print 'go' print 'print ''Detach of ' + upper(@dbname) + ' database successfully completed''' print 'go' PRINT ' ' FETCH NEXT FROM rs_cursor INTO @dbname END CLOSE rs_cursor deallocate rs_cursor print ' ' print 'print ''SERVER NAME : ' + upper(@server_name) + '--> All databases successfully detached''' Copy the above script and execute it, you will get the output as detach script . Copy the output and execut it. All the user db's will be detached!!!!
If you face any problem let me know
Regards..Vidhya Sagar SQL-Articles
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, July 24, 2009 11:39 PM
Points: 2,
Visits: 13
|
|
Great time saver. I have to move mdf files for 198 databases and this is perfect. One thing I added was below Alter command to drop connections prior to detach.
print 'ALTER DATABASE ' + @dbname + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'
print 'go'
Also, since I'm reattaching DBs from new file location, I used your script and replaced print commands with below to attach the DBs. Just made sure tor run both scripts before detaching so you have the Detach and Attach scripts needed:
print 'USE [master]' print 'GO' print 'CREATE DATABASE ' + @dbname + ' ON' print '( FILENAME = N'''+ 'E:\MSSQL2K5\Data\' + @dbname + '.mdf''' + '),' print '( FILENAME = N'''+ 'F:\MSSQL2K5\log\' + @dbname + '_log.ldf''' + ')' print 'FOR ATTACH' print 'GO'
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, August 26, 2010 12:05 PM
Points: 4,
Visits: 31
|
|
Hey,
What was the final script that you came up with...?
Daniel
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, July 24, 2009 11:39 PM
Points: 2,
Visits: 13
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, December 10, 2012 8:25 AM
Points: 8,
Visits: 128
|
|
Ed Bob, Thanks for the handy script. I have several servers with hundreds of dbs and need to reconfigure SAN drives.
DD
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, February 28, 2013 10:29 PM
Points: 1,
Visits: 3
|
|
I ahve modified the querry as the execution satement was missing in it.
set nocount on declare @dbname as varchar(80) declare @server_name as varchar(20) select @server_name = @@servername declare rs_cursor CURSOR for select name from master.dbo.sysdatabases where name not in ('model','master','msdb','tempdb','alert_db','mssecurity') open rs_cursor Fetch next from rs_cursor into @dbname IF @@FETCH_STATUS <> 0 PRINT 'No database to backup...Please check your script!!!' WHILE @@FETCH_STATUS = 0 BEGIN Exec sp_detach_db @dbname --- This Line was missing in the querry included that and it worked wonderfully print 'go' print 'print ''Detach of ' + upper(@dbname) + ' database successfully completed''' print 'go' PRINT ' ' FETCH NEXT FROM rs_cursor INTO @dbname END CLOSE rs_cursor deallocate rs_cursor print ' ' print 'print ''SERVER NAME : ' + upper(@server_name) + '--> All databases successfully detached'
any querries mail me @ sri6sri6@yahoo.com. I will try to provide my help ASAP.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 12:38 AM
Points: 37,725,
Visits: 29,981
|
|
sri6sri6 (12/10/2012) I ahve modified the querry as the execution satement was missing in it.
It wasn't missing. The script you refer to generated the sp_detach statements, allowing someone to copy the output, check it over and then run it.
p.s. 4 year old thread.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|