如何将变量添加到批处理文件?
问题描述:
大家好,我是新的批处理文件和挣扎。我正在使用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
等效这是一种享受。关于你更好的解决方案,我不理解它。我看到你在循环播放,但%% f让我困惑。可以解释一下。 – user9969 2010-10-02 10:33:42
for命令有很多很好的问题:http://stackoverflow.com/search?q=for+loop+batch – 2010-10-02 10:37:49