Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Passing ServerName as parameter to SQLCMD Expand / Collapse
Author
Message
Posted Friday, November 6, 2009 8:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 31, 2010 1:05 PM
Points: 44, Visits: 245
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!!!
Post #814969
Posted Friday, November 6, 2009 8:56 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 8:15 AM
Points: 653, Visits: 1,428
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.
Post #814974
Posted Friday, November 6, 2009 9:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 31, 2010 1:05 PM
Points: 44, Visits: 245
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!!!
Post #815044
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse