Solved

Detach multiple databases

Posted on 2011-09-28
6
240 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
Comment Utility
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
Comment Utility
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
Comment Utility
error:

Incorrect syntax near 'sp_detach_db'.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 15

Accepted Solution

by:
Anuj earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Save your sql script file then call the following in Powershell

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now