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:表示可以是一个sys.messages表中定义的消息代号;
              使用 sp_addmessage 存储在 sys.messages 目录视图中的用户定义错误消息号。
              用户定义错误消息的错误号应当大于 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]代码示例

--示例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)
 

--示例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

--示例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

--示例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   

--A. 从 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;

--示例6

--B. 在 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

--示例7

--C. 使用局部变量提供消息文本
以下代码示例显示如何使用局部变量为 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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

点赞(205)

评论列表共有 0 条评论

立即
投稿
返回
顶部