Solved

Detach multiple databases

Posted on 2011-09-28
6
244 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Make Temp Table Query Faster 5 41
Run SQL Server Proc from Access 11 30
SQL Improvement  ( Speed) 14 27
ms sql + get number in list out of total 7 29
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

770 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