码迷,mamicode.com
首页 > 其他好文 > 详细

使用 Throw 抛出错误消息

时间:2016-07-12 23:22:10      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

Throw 命令没有Exception severity 参数,The exception severity is always set to 16.

Raises an exception and transfers execution to a CATCH block of a TRY…CATCH construct in SQL Server 2012.

THROW [ { error_number | @local_variable },
        { message | @local_variable },
        { state | @local_variable } ] 
[ ; ]

Arguments  

error_number                               

Is a constant or variable that represents the exception. error_number is int and must be greater than or equal to 50000 and less than or equal to 2147483647.

message                               

Is an string or variable that describes the exception. message is nvarchar(2048).

state                               

Is a constant or variable between 0 and 255 that indicates the state to associate with the message. state is tinyint.

Remarks

If a TRY…CATCH construct is not available, the session is ended. The line number and procedure where the exception is raised are set. The severity is set to 16.

If the THROW statement is specified without parameters, it must appear inside a CATCH block. This causes the caught exception to be raised. Any error that occurs in a THROW statement causes the statement batch to be ended.

 

State的作用:如果在多个位置引发相同的用户自定义错误,则针对每个位置使用唯一的状态号有助于找到引发错误的代码段。

 

Examples

THROW 51000, The record does not exist., 1;

 

参考文档:

THROW (Transact-SQL)

使用 Throw 抛出错误消息

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5665088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!