SQLServer中RAISERROR的用法详细介绍

SQLServer中RAISERROR的用法详细介绍

2023年7月13日发(作者:)

SQLServer中RAISERROR的⽤法详细介绍 SQL Server 中 RAISERROR 的⽤法raiserror 的作⽤: raiserror 是⽤于抛出⼀个错误。[ 以下资料来源于sql server 2005的帮助 ]其语法如下:RAISERROR ( { msg_id | msg_str | @local_variable }

{ ,severity ,state }

[ ,argument [ ,...n ] ]

)

[ WITH option [ ,...n ] ]简要说明⼀下: 第⼀个参数:{ msg_id | msg_str | @local_variable } msg_id:表⽰可以是⼀个es表中定义的消息代号; 使⽤ sp_addmessage 存储在 es ⽬录视图中的⽤户定义错误消息号。 ⽤户定义错误消息的错误号应当⼤于 50000。 msg_str:表⽰也可以是⼀个⽤户定义消息,该错误消息最长可以有 2047 个字符; (如果是常量,请使⽤N'xxxx',因为是nvarchar的) 当指定 msg_str 时,RAISERROR 将引发⼀个错误号为 5000 的错误消息。 @local_variable:表⽰也可以是按照 msg_str ⽅式的格式化字符串变量。

第⼆个参数:severity ⽤户定义的与该消息关联的严重级别。(这个很重要) 任何⽤户都可以指定 0 到 18 之间的严重级别。 [0,10]的闭区间内,不会跳到catch; 如果是[11,19],则跳到catch; 如果[20,⽆穷),则直接终⽌数据库连接;第三个参数:state 如果在多个位置引发相同的⽤户定义错误, 则针对每个位置使⽤唯⼀的状态号有助于找到引发错误的代码段。

介于 1 ⾄ 127 之间的任意整数。(state 默认值为1) 当state 值为 0 或⼤于 127 时会⽣成错误!第四个参数:argument ⽤于代替 msg_str 或对应于 msg_id 的消息中的定义的变量的参数。第五个参数:option 错误的⾃定义选项,可以是下表中的任⼀值: LOG :在错误⽇志和应⽤程序⽇志中记录错误; NOWAIT:将消息⽴即发送给客户端; SETERROR:将 @@ERROR 值和 ERROR_NUMBER 值设置为 msg_id 或 50000;[SQL]代码⽰例--⽰例1DECLARE @raiseErrorCode nvarchar(50)SET @raiseErrorCode = CONVERT(nvarchar(50), YOUR UNIQUEIDENTIFIER KEY)RAISERROR('%s INVALID ID. There is no record in table',16,1, @raiseErrorCode)

--⽰例2RAISERROR ( N'This is message %s %d.', -- Message text, 10, -- Severity, 1, -- State, N'number', -- First argument. 5 -- Second argument. );

-- The message text returned is: This is message number --⽰例3RAISERROR (N'<<%*.*s>>', -- Message text. 10, -- Severity, 1, -- State, 7, -- First argument used for width. 3, -- Second argument used for precision. N'abcde'); -- Third argument supplies the string.-- The message text returned is: << abc>>.GO--⽰例4RAISERROR (N'<<%7.3s>>', -- Message text. 10, -- Severity, 1, -- State, N'abcde'); -- First argument supplies the string.-- The message text returned is: << abc>>.GO--⽰例5

--A. 从 CATCH 块返回错误消息以下代码⽰例显⽰如何在 TRY 块中使⽤ RAISERROR 使执⾏跳⾄关联的 CATCH 块中。它还显⽰如何使⽤ RAISERROR 返回有关调⽤ CATCH 块的错误的信息。BEGIN TRY RAISERROR ('Error raised in TRY block.', -- Message text. 16, -- Severity. 1 -- State. );END TRYBEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT

@ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. );END CATCH;--⽰例6--B. 在 es 中创建即席消息以下⽰例显⽰如何引发 es ⽬录视图中存储的消息。该消息通过 sp_addmessage 系统存储过程,以消息号50005添加到 es ⽬录视图中。sp_addmessage @msgnum = 50005, @severity = 10, @msgtext = N'<<%7.3s>>';GORAISERROR (50005, -- Message id. 10, -- Severity, 1, -- State, N'abcde'); -- First argument supplies the string.-- The message text returned is: << abc>>.GOsp_dropmessage @msgnum = 50005;GO--⽰例7--C. 使⽤局部变量提供消息⽂本以下代码⽰例显⽰如何使⽤局部变量为 RAISERROR 语句提供消息⽂本。sp_addmessage @msgnum = 50005, @severity = 10, @msgtext = N'<<%7.3s>>';GORAISERROR (50005, -- Message id. 10, -- Severity, 1, -- State, N'abcde'); -- First argument supplies the string.-- The message text returned is: << abc>>.GOsp_dropmessage @msgnum = 50005;GO感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689246945a225641.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信