Passing ServerName as parameter to SQLCMD

  • Hi,

    is it possible to pass the servername as parameter to SQLCMD.

    For eg: SQLCMD -E -S %servername% -i <input file> -o <output file>

    servername is the parameter which i wish to pass..

    This is basically to get some information from mulitple servers using SQLCMD

    any help is appreciated!!!

    Thanks!!!

  • I do this all of the time using two batch files. The first one contains a %1 value for the server name, similar to below:

    run.bat:

    sqlcmd -S %1 -E -i "X:\script.sql" -o "X:\%1_scriptout.txt"

    The second one then calls this one passing in the name of the server as a parameter:

    DeployAllServers.bat:

    call run.bat auca-sql02

    call run.bat auca-sql01

    call run.bat auca-sql01

    I use it to deploy the same scripts to dozens of servers at a time and it works pretty well for that.

  • Thanks George.. That script work very well.. But I got an issue.

    I run the SQLCMD for named instances too.. Suppose, when i execute my script again a named instance say "serverA\SQL2K". The below stmt

    sqlcmd -S %1 -E -i "X:\script.sql" -o "X:\%1_scriptout.txt"

    will be executed as

    sqlcmd -S serverA\SQL2K -E -i "X:\script.sql" -o "X:\serverA\SQL2K_scriptout.txt"

    This gives an error because the path "X:\serverA\SQL2K_scriptout.txt" does not exist.

    Thanks!!!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply