| How to execute multiple scripts on multiple sql servers |
|
Very often DBA needs to deploy a bunch of transact-sql scripts to multiple servers. There is a way to open each server in SQL Server Management Studio and than run each t-sql script on each sql server sequentially. Few things in DBA job may be more annoying than this especially if there are 50+ scripts and more than 10 servers. Another option is to run all these scripts automatically on all the servers using sqlcmd, osql or isql depending on the sql server version or to use new feature of SQL Server Management Studio for sql server 2008 - query against a group of registered servers. This feature is especially useful if you need to see the outputs of your queries but when you need to run a bunch of queries it is inconvenient open them in SSMS one by one. Running multiple scripts against group of servers using sql server command line utilities (sqlcmd or osql) All you need is to create a batch file and put all your scripts in a single folder. The next batch file will run all the scripts from the current folder (where .bat file is placed) setlocal Give this file a name, for instance sql.bat. Now place the file into the folder containing the scripts you need to run againsts the group of servers. The given example runs scripts with .sql file extension from current folder against two local instances (sql2008 and sql2008i2) you should create as many setlocal/endlocal blocks as the number of sql servers you need to run your queries against. The given example uses windows authentication or trusted connection in other words (-E parameter) if you need to use SQL authentication - use -U and-P parameters or address sqlcmd help (run sqlcmd -?). Outputs of the queries may be stored to a file using -o param. SQLCMD utility substitutes ISQL in sql server 2005. If some of instances run earlier version of sql server (sql server 2000 or sql server 7) - replace sqlcmd with osql or isql. If the order of scripts execution is critical (for instance one group of files creates tables and another group fills them in) this order must be reflected in the file names of t-sql scripts. Comments (1) |