SQL Server – Apply SQL Script against a few servers

Microsoft – SQL Server – Apply SQL Script against a few servers

Sometimes all you need to do is apply a few SQL Server Scripts against a few servers:

Here is one way of doing so:

1) Prepare a text file

2) Prepare a command file

3) Run command file

Textfile (serverlist.txt):

DBSales
DBLab
DBHR

Prepare Command file (launch.cmd):


for /f %%s in (type serverlist.txt) do (

  sqlcmd -i createtesttables.sql -S%%s -Ulabuser -P60sflowerchoild
  sqlcmd -i addtestdata.sql -S%%s -Ulabuser -P60sflowerchoild
)

 

References:

  1. Loop command: against a set of files – conditionally perform a command against each item.
    http://ss64.com/nt/for_f.html

  2. For Command
    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

  3. Two Tricks for your scripting toolbet
    http://www.windowsitpro.com/article/server-management/two-tricks-for-your-scripting-toolbelt

  4. For Loops – Basic Syntax
    http://www.robvanderwoude.com/for.php
  5. Windows batch command(s) to read first line from text file
    http://stackoverflow.com/questions/130116/windows-batch-commands-to-read-first-line-from-text-file

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s