中斷 SQL Server 腳本執行的政策
在執行過程中停止 SQL Server 腳本通常是必要的,特別是在驗證或資料查找操作期間。 以下是實現這一目標的有效方法:
1。 raiserror
方法:
raiserror
語句提供了一種停止腳本執行的強大方法。 使用以下語法:
raiserror('Critical error encountered', 20, -1) with log
將嚴重性等級設為 20(或更高)並包含 WITH LOG
將終止腳本並將錯誤記錄在 SQL Server 日誌中。
2。利用noexec
選項:
此方法與 GO
語句結合使用。 設定 noexec on
會阻止執行後續指令。請記得在恢復執行之前關閉noexec
。
結合兩種方法的說明範例:
print 'Starting script execution...' go print 'Fatal error detected, script will terminate!' -- Method 1: Raiserror raiserror('Critical error encountered', 20, -1) with log -- Method 2: Noexec set noexec on print 'This message will not be printed.' go -- Re-enable execution (necessary in SSMS for script re-run) set noexec off
重要注意事項:
raiserror
需要管理員權限並關閉資料庫連線。 noexec
不會關閉連接,但需要明確重新啟用執行。 sqlcmd.exe
執行時,2745 退出代碼表示腳本終止。 以上是如何在驗證或尋找過程中有效停止 SQL Server 腳本執行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!