Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select which server to run script against 1

Status
Not open for further replies.

KirbyWallace

Programmer
Dec 22, 2008
65
US
I have a script that does various maintenance things to a database that's kept on a backup server. It resets all passwords to blanks, empties certain temp tables, drops and readds all users, etc etc. It creates a test system from a backup of the live system.

Code:
USE [master]
ALTER DATABASE [ips_pathway_services] SET OFFLINE WITH ROLLBACK IMMEDIATE
GO


It would be a disaster if this script were run against the live database server. But SSMS makes it VERY easy to do jsut that. (Granted I have the custom colour bar in the query window to "help")...

What I need is a way to USE [server] in much the same way that I can USE [database]. That way I can code the script so that it cannot run against the live server.

Is there such a thing?

Thanks


 
How about this....

Code:
If @@ServerName = '[!]NameOfYourServer[/!]'
  Alter Database [ips_pathway_services] Set OFFLINE WITH ROLLBACK IMMEDIATE

There's nothing that I am aware of that will connect you to a different server.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
If you run the query in SQLCMD mode (within SSMS) you can switch servers vis the :CONNECT statement.

Code:
:CONNECT YourDevServer
USE SomeDatabase
GO
...

Denny
MVP
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / SQL 2005 BI / SQL 2008 DBA / SQL 2008 DBD / SQL 2008 BI / MWSS 3.0: Configuration / MOSS 2007: Configuration)
MCITP (SQL 2005 DBA / SQL 2008 DBA / SQL 2005 DBD / SQL 2008 DBD / SQL 2005 BI / SQL 2008 BI)

My Blog
 
MrDenny's answer is technically (and exactly) what I was asking for, but George (gmmastors) gives me another idea that I actually prefer.


If I attempt to run this script on anything other than my test server, I do not want it to correctly switch to the dev server and continue (although that's great, and that's exactly what I asked you for help with).

I want it to fail, but I also want to know about it. I want that feedback that says "You just attempted to destroy your company's financial future... Luckily, I stopped you just in the nick of time..." ;-)

That kind of feedback helps train my "bio-neural network" (ie, my brain) in ways that "auto-correcting and continuing" does not.

So, Thanks, to both of you.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top