2013-10-04 43 views
1

當運行下面的命令,sqlcmd.exe寫道:「超時過期」到控制檯,但它返回的退出代碼爲0如何處理sqlcmd.exe「Timeout expired」消息,該消息似乎不是錯誤?

sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerThan5Seconds.sql" -E 

請注意,我特意設置的QueryTimeout,-t,5秒。另外,我是-b參數,我認爲它應該返回退出代碼1的錯誤。我也嘗試了-r1參數和-m-1參數無濟於事。

我也看過Troubleshooting: Timeout Expired文章,並注意到它引用了「Error:-2」。所有與錯誤相關的參數的sqlcmd.exe文檔似乎只處理大於0的錯誤代碼,因此「Timeout expired」可能不是錯誤?另外,無論我嘗試過哪些參數,我只看到「Timeout expired」作爲輸出,根本沒有錯誤代碼。

最後,這裏是我試過的所有命令,只有編輯,以保護服務器和數據庫名稱:

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E 
Timeout expired 

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r1 
Timeout expired 

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V16 
Timeout expired 

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1 
Timeout expired 

C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1 
Timeout expired 

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r0 
Timeout expired 

C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -o "sqlError.txt" 

C:\>notepad sqlError.txt 

#REM only "Timeout expired" was written to sqlError.txt 

C:\>del sqlError.txt 

C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1 
Timeout expired 

C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V-3 
Sqlcmd: '-V -3': Severity level has to be a number between 1 and 25. 

C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V1 
Timeout expired 

C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r1 
Timeout expired 

更新:我應該注意到我是從通過C#控制檯應用程序調用此Systems.Diagnostics.Process,所以我想如果可能的話ExitCode是1。我不確定我是否可以捕獲ERRORLEVEL。

更多更新:由於某種原因,我的機器正在將stderr寫入stdout,所以我無法測試Stefan M的答案的有效性。我相信他的回答很可能是正確的,因爲我已經以相同的方式解釋了sqlcmd.exe的文檔。我還有一個關於爲什麼我的機器正在寫stderr到標準輸出here的問題。

回答

1

您不能使用-V-3,最低值爲-V1。結合-b你應該得到想要的結果。只有當嚴重性高於-V值時,「ERRORLEVEL」值纔會被設置爲1。

我最好的猜測是(未測試):

sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerThan5Seconds.sql" -E -V1 

然後再試試:

echo %ERRORLEVEL% 

相關部分是:

The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V

http://technet.microsoft.com/en-us/library/ms162773.aspx

-b

Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd does not report errors for severity level 10 (informational messages).

If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, ERRORLEVEL returned is 1.

-m error_level

Controls which error messages are sent to stdout. Messages that have a severity level greater than or equal to this level are sent. When this value is set to -1, all messages including informational messages, are sent. Spaces are not allowed between the -m and -1. For example, -m-1 is valid, and -m -1 is not.

This option also sets the sqlcmd scripting variable SQLCMDERRORLEVEL. This variable has a default of 0.

-V error_severity_level

Controls the severity level that is used to set the ERRORLEVEL variable. Error messages that have severity levels greater than or equal to this value set ERRORLEVEL. Values that are less than 0 are reported as 0. Batch and CMD files can be used to test the value of the ERRORLEVEL variable.

+0

謝謝你的回答。你說的-V-3是不正確的,我以前的測試沒有-b和-V1的組合。我只是嘗試了沒有骰子的組合。仍然退出代碼0和標準輸出「超時已過期\ r \ n」。謝謝你嘗試。 – JustinP8

+0

'ERRORLEVEL'也是空的? –

+0

實際上我不確定。我更新了提到的問題,我使用System.Diagnostics.Process通過C#調用sqlcmd.exe。 – JustinP8