[英]How do I add the content of an environment variable to a file in windows batch?
[英]How can I add a variable to a batch file?
大家好,我是新來的批處理文件和掙扎。我正在使用sql server 2008
我創建了一個執行其他批處理文件+ sql腳本的批處理文件。 我想使用變量該怎么辦? 我想將路徑設置為變量。 根據腳本的位置,我可以有多個路徑。
:On Error exit
CALL C:\mypath\Scripts\ExecSqlScripts.bat
CALL C:\mypath\Scripts\ExecSqlScriptsTwo.bat
SQLCMD -S (Local) -i C:\mypath\Scripts\InsertUsernameTwo.sql
SQLCMD -S (Local) -i C:\mypath\Scripts\InsertUsernameThree.sql
SQLCMD -S (Local) -i C:\mypath\Scripts\InsertUsernameFour.sql
有什么建議么?
您需要set
命令-http: //www.computerhope.com/sethlp.htm
:On Error exit
set thePath=C:\mypath\Scripts
CALL %thePath%\ExecSqlScripts.bat
CALL %thePath%\ExecSqlScriptsTwo.bat
SQLCMD -S (Local) -i %thePath%\InsertUsernameTwo.sql
SQLCMD -S (Local) -i %thePath%\InsertUsernameThree.sql
SQLCMD -S (Local) -i %thePath%\InsertUsernameFour.sql
也可以從批處理文件外部調用thePath=C:\\mypath\\Scripts
。
避免重復代碼使用for命令的另一種解決方案。
for %%f in (ExecSqlScripts ExecSqlScriptsTwo) do call %%f.bat
這等效於CALL%thePath%\\ ExecSqlScripts.bat CALL%thePath%\\ ExecSqlScriptsTwo.bat
for %%f in (InsertUsernameTwo InsertUsernameThree InsertUsernameFour) do call SQLCMD -S (Local) -i %thePath%\%%f.sql
這相當於
SQLCMD -S (Local) -i %thePath%\InsertUsernameTwo.sql
SQLCMD -S (Local) -i %thePath%\InsertUsernameThree.sql
SQLCMD -S (Local) -i %thePath%\InsertUsernameFour.sql
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.