Solved

Detach multiple databases

Posted on 2011-09-28
6
245 Views
Last Modified: 2012-05-12
Hello

I want to use a script to detach mulitple databases, starting with the name eqs.

I have found a script to use that should have the functions i need for this. Although this script does not work "standalone" without removing the "print" before each command.

I was hoping for help to do this without the "print" commands, running the script, and make it work :)

Does anyone know what changes i can make to this script to make it work?

And is it possible to run this script from a Powershell script or something like that?
 
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 like ('eqs%') AND name not in ('eqs_test', 'eqs_log')
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'''

Open in new window

0
Comment
Question by:frankEQS
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 36716049
Append char(10)


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 like ('eqs%') AND name not in ('eqs_test', 'eqs_log')
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+ CHAR(10) + 'GO'
PRINT ' '

print 'print ''Detach of ' + upper(@dbname) + ' database successfully completed'''+ CHAR(10) + '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'''
0
 
LVL 15

Expert Comment

by:Anuj
ID: 36716060
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 like ('eqs%') AND name not in ('eqs_test', 'eqs_log')
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
         sp_detach_db @dbname
         print 'Detach of ' + upper(@dbname) + ' database successfully completed'

PRINT ' '
FETCH NEXT FROM rs_cursor INTO @dbname
END
CLOSE rs_cursor
deallocate rs_cursor
print ' '

print 'SERVER NAME : ' + upper(@server_name) + '--> All databases successfully detached'
0
 

Author Comment

by:frankEQS
ID: 36716088
error:

Incorrect syntax near 'sp_detach_db'.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 15

Accepted Solution

by:
Anuj earned 500 total points
ID: 36716172
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 like ('eqs%') AND name not in ('eqs_test', 'eqs_log')
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
         print 'Detach of ' + upper(@dbname) + ' database successfully completed'

PRINT ' '
FETCH NEXT FROM rs_cursor INTO @dbname
END
CLOSE rs_cursor
deallocate rs_cursor
print ' '

print 'SERVER NAME : ' + upper(@server_name) + '--> All databases successfully detached'
0
 

Author Comment

by:frankEQS
ID: 36716448
Yes this works!

How can i run this SQL query from a powershell script?
0
 
LVL 15

Assisted Solution

by:Anuj
Anuj earned 500 total points
ID: 36716797
Save your sql script file then call the following in Powershell

invoke-sqlcmd –ServerInstance <ServerName> -Database <DBName> -InputFile <SQL Scriptfile Path>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question