2014-11-21 73 views
1

我正在調用批處理文件上的SQL Server腳本,但我需要在批處理文件中獲取錯誤(當腳本失敗時),我該怎麼辦?如何在批處理文件中獲取SQL Server腳本錯誤文件

這是批處理文件:

sqlcmd -S HOST -U User -P password -i test.sql 
echo %errorlevel% 

,這是腳本文件(的Test.sql):

USE TrainingSitecore_Web 
go 

SELECT * FROM items 
go 

RAISERROR ('This is a test Error.',-- Message text. 
      16,-- Severity. 
      1 -- State. 
); 

有意義嗎?

+1

將在下面的鏈接最後的答案是你的情況? http://dba.stackexchange.com/questions/77298/how-to-make-sqlcmd-return-an-errorlevel-other-than-0-when-the-sql-script-fails – knkarthick24 2014-11-21 17:37:40

+0

是的男人,它是工作:) – 2014-11-21 18:32:10

回答

0

它可以使用-r參數來完成:

實施例:

sqlcmd -Q "select 1 as a; select 1/0 as b" -E -r1 1> NUL 

-r [0 | 1] msgs到stderr

將錯誤信息輸出重定向到屏幕(stderr)。如果確實 未指定參數,或者指定的值爲0,則只有 嚴重性級別爲11或更高的錯誤消息被重定向。如果指定 1,則包括PRINT在內的所有錯誤消息輸出都將被重定向。如果使用-o,則沒有 效果。默認情況下,消息被髮送到標準輸出。

MSDN for more

+0

謝謝,但不工作,錯誤級別仍然在0 :(請看http://postimg.org/image/95wwn7p3z/ – 2014-11-21 17:04:58

相關問題