2010-10-02 71 views
0

大家好,我是新的批處理文件和掙扎。我正在使用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 

有什麼建議嗎?

回答

2

您需要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 
+0

等效這是一種享受。關於你更好的解決方案,我不理解它。我看到你在循環播放,但%% f讓我困惑。可以解釋一下。 – user9969 2010-10-02 10:33:42

+0

for命令有很多很好的問題:http://stackoverflow.com/search?q=for+loop+batch – 2010-10-02 10:37:49

相關問題