SQL 抛出异常错误信息- RAISERROR (存储过程)

SQL 抛出异常错误信息- RAISERROR  (存储过程)

1、描述:生成错误消息并启动会话的错误处理。RAISERROR可以引用存储在系统消息目录视图或动态生成消息。该消息将作为服务器错误消息返回到调用应用程序或TRY…CATCH构造的关联CATCH块。新应用程序应该改用THROW。

2、作用: raiserror 是用于抛出一个错误。

3、语法:

RAISERROR ( { msg_id | msg_str | @local_variable }        
            { ,severity ,state }        
            [ ,argument [ ,...n ] ] 
          )       
   [ WITH option [ ,...n ] ]

4、参数说明:

参数1:{ msg_id | msg_str | @local_variable }

      msg_id:表示可以是一个sys.messages表中定义的消息代号;

              使用 sp_addmessage 存储在 sys.messages 目录视图中的用户定义错误消息号。

              用户定义错误消息的错误号应当大于 50000。

     msg_str:表示也可以是一个用户定义消息,该错误消息最长可以有 2047 个字符;

             (如果是常量,请使用N'xxxx',因为是nvarchar的)

              当指定 msg_str 时,RAISERROR 将引发一个错误号为 5000 的错误消息。

     @local_variable:表示也可以是按照 msg_str 方式的格式化字符串变量。     

参数2:severity

            用户定义的与该消息关联的严重级别。(这个很重要)

            任何用户都可以指定 0 到 18 之间的严重级别。

            [0,10]的闭区间内,不会跳到catch;

            如果是[11,19],则跳到catch;

            如果[20,无穷),则直接终止数据库连接;

参数3:state

            如果在多个位置引发相同的用户定义错误,

            则针对每个位置使用唯一的状态号有助于找到引发错误的代码段。

            介于 1 至 127 之间的任意整数。(state 默认值为1)

            当state 值为 0 或大于 127 时会生成错误!

参数4:argument

            用于代替 msg_str 或对应于 msg_id 的消息中的定义的变量的参数。

参数5:option

            错误的自定义选项,可以是下表中的任一值:

            LOG :在错误日志和应用程序日志中记录错误;

            NOWAIT:将消息立即发送给客户端;

            SETERROR:将 @@ERROR 值和 ERROR_NUMBER 值设置为 msg_id 或 50000;

 

5、SQL代码示例

5.1 

DECLARE @raiseErrorCode nvarchar(50)
SET @raiseErrorCode = CONVERT(nvarchar(50), YOUR UNIQUEIDENTIFIER KEY)
RAISERROR('%s INVALID ID. There is no record in table',16,1, @raiseErrorCode)

5.2

RAISERROR (
             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 5.
GO

5.3

RAISERROR (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

5.4

RAISERROR (N'<<%7.3s>>', -- Message text.
           10,           -- Severity,
           1,            -- State,
           N'abcde');    -- First argument supplies the string.
-- The message text returned is: <<    abc>>.
GO

5.5   从 CATCH 块返回错误消息

以下代码示例显示如何在 TRY 块中使用 RAISERROR 使执行跳至关联的 CATCH 块中。

它还显示如何使用 RAISERROR 返回有关调用 CATCH 块的错误的信息。

BEGIN TRY
    RAISERROR ('Error raised in TRY block.', -- Message text.
                16, -- Severity.
                1 -- State.
               );
END TRY
BEGIN 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;

5.6 在 sys.messages 中创建即席消息

以下示例显示如何引发 sys.messages 目录视图中存储的消息。

该消息通过 sp_addmessage 系统存储过程,以消息号50005添加到 sys.messages 目录视图中。

sp_addmessage @msgnum = 50005,
               @severity = 10,
               @msgtext = N'<<%7.3s>>';
GO
RAISERROR (50005, -- Message id.
           10,    -- Severity,
           1,     -- State,
           N'abcde'); -- First argument supplies the string.
-- The message text returned is: <<    abc>>.
GO
sp_dropmessage @msgnum = 50005;
GO

5.7 使用局部变量提供消息文本

以下代码示例显示如何使用局部变量为 RAISERROR 语句提供消息文本。

sp_addmessage @msgnum = 50005,
              @severity = 10,
              @msgtext = N'<<%7.3s>>';
GO

RAISERROR (50005, -- Message id.
           10,    -- Severity,
           1,     -- State,
           N'abcde'); -- First argument supplies the string.
-- The message text returned is: <<    abc>>.
GO

sp_dropmessage @msgnum = 50005;

GO

  

创建时间:2020.10.29  更新时间:2021.02.05

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/13896823.html