Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Detach multiple databases

Posted on 2011-09-28
6
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 15

Accepted Solution

by:
Anuj earned 2000 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.

610 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