SQL SERVER中獲取錯誤號

使用ERROR_NUMBER函數;

Return Types

int

Return Value

When called in a CATCH block, ERROR_NUMBER returns the error number of the error that caused the CATCH block to run.

ERROR_NUMBER returns NULL when called outside the scope of a CATCH block.

Remarks

ERROR_NUMBER supports calls anywhere within the scope of a CATCH block.

ERROR_NUMBER returns a relevant error number regardless of how many times it runs, or where it runs within the scope of the CATCH block. This contrasts with a function like @@ERROR, which only returns an error number in the statement immediately following the one that causes an error.

In a nested CATCH block, ERROR_NUMBER returns the error number specific to the scope of the CATCHblock that referenced that CATCH block. For example, the CATCH block of an outer TRY...CATCH construct could have an inner TRY...CATCH construct. Inside that inner CATCH block, ERROR_NUMBERreturns the number of the error that invoked the inner CATCH block. If ERROR_NUMBER runs in the outer CATCH block, it returns the number of the error that invoked that outer CATCH block.

 

這裏注意的是,ERROR_NUMBER實際上就是sys.messages視圖中的message_id字段,所以可以參照這個表瞭解sql server拋出異常的情況;

注意sys.messages視圖中統一個message_id可能會有多條記錄,因爲存在不同的語言;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章