Solved

Detach multiple databases

Posted on 2011-09-28
6
246 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

726 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