SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Passing ServerName as parameter to SQLCMD


Passing ServerName as parameter to SQLCMD

Author
Message
karthik -450760
karthik -450760
Old Hand
Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)

Group: General Forum Members
Points: 322 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!!!
George M Parker
George M Parker
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1600 Visits: 1472
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.
karthik -450760
karthik -450760
Old Hand
Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)Old Hand (322 reputation)

Group: General Forum Members
Points: 322 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!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search