Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer from Martin Smaith is my prefered solution but I can suggest 2 other ways:</p> <p>1) You can use the "SQLCMD Mode" in SSMS.</p> <p>It would look like:</p> <pre><code>:connect MyServer -U MyLogin -P MyComplexPassword --Then have some code :connect MyOtherServer ... --Then have some code </code></pre> <p>But you can't loop that so you will have repetitive code.</p> <p>Or</p> <p>2) You can create a CMD file with a FOR loop based on a list of servers. Here's an example of my own brew. Just Copy Paste it to REQ.CMD and create a text file named SERVERS.LST with a server name per line. Connection will be Windows Authenticated. You can change OSQL for SQLCMD if you like but OSQL should work with SQL 2008.</p> <pre><code>REM To execute a SQL command or file to multiple servers REM Using a server list file REM And using 2 kind of parameters (script file or query) REM Pollus Brodeur - June 2007 @echo off set PARAM1=%1 set PARAM2=%2 set PARAM3=1 REM You need at least 1 parameter if x==%1x goto ERROR1 if EXIST %PARAM1% (set ISFILE=1) else (set ISFILE=0) REM Setting Servers List File and verify file existance or X if x==%2x (set PARAM2=servers.lst) if X==%2 (set PARAM3=0 &amp; set PARAM2=servers.lst &amp; goto MAIN) if NOT EXIST %PARAM2% (goto ERROR2) REM Not interactive if X==%3 (set PARAM3=0) :MAIN echo =============================================================================== echo REQ - Execute a SQL command or file to multiple servers using a list file echo %DATE% %TIME% if %PARAM3%==0 (echo Non Interactive mode) echo =============================================================================== if %ISFILE%==0 (GOTO COMMAND) echo For following file : echo %PARAM1% goto EXEC :COMMAND echo For following command : echo %PARAM1% :EXEC echo =============================================================================== echo For following servers : type %PARAM2% echo =============================================================================== if %PARAM3%==1 (echo Interactive mode. Use X parameter to deactivate &amp; pause) echo Exec... if NOT EXIST .\out mkdir out if EXIST out\req.out (del out\req.out) if %ISFILE%==0 (GOTO COMMAND1) for /F %%a in (%PARAM2%) do echo %%a &amp; osql -S %%a -E -n -h-1 -w 9999 -u -i %PARAM1% -o out\req.out.%%a.txt goto ENDEXEC :COMMAND1 for /F %%a in (%PARAM2%) do echo %%a &amp; osql -S %%a -E -n -h-1 -w 9999 -u -Q %PARAM1% -o out\req.out.%%a.txt :ENDEXEC type out\req.out.*.txt &gt;&gt; out\req.out 2&gt;&amp;1 echo =============================================================================== echo Execution is done. Output files are in .\out echo more /E /C out\req.out.*.txt echo more out\req.out echo =============================================================================== if %PARAM3%==1 (pause) goto END :ERROR1 goto CALLEXAMPLE :ERROR2 echo Error: Server File List named %PARAM2% doesn't exists goto CALLEXAMPLE :CALLEXAMPLE echo -------------------= REQ call examples =-------------------- echo REQ.cmd Param1 Param2 Param3 echo Param1 = command or scriptfile.sql {mandatory} echo Param2 = servers list file {servers.lst per default} echo Param3 = X {for non interactive mode} echo ------------------------------------------------------------ :END </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload