SQL Server 中 RAISERROR 的用法

raiserror 的作用: raiserror 是用于抛出一个错误

 

 

其语法如下:

1 RAISERROR ( { msg_id | msg_str | @local_variable }        
2             { ,severity ,state }        
3             [ ,argument [ ,...n ] ] 
4           )       
5    [ 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]代码示例
egg1:
1 DECLARE @raiseErrorCode nvarchar(50)
2 SET @raiseErrorCode = CONVERT(nvarchar(50), YOUR UNIQUEIDENTIFIER KEY)
3 RAISERROR('%s INVALID ID. There is no record in table',16,1, @raiseErrorCode)

egg2:

1 RAISERROR (
2              N'This is message %s %d.', -- Message text,
3              10,                        -- Severity,
4              1,                         -- State,
5              N'number',                 -- First argument.
6              5                          -- Second argument.
7           ); 
8 -- The message text returned is: This is message number 5.
9 GO

egg3:

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

egg4:

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

egg5:

--A. 从 CATCH 块返回错误消息
以下代码示例显示如何在 TRY 块中使用 RAISERROR 使执行跳至关联的 CATCH 块中。
它还显示如何使用 RAISERROR 返回有关调用 CATCH 块的错误的信息。

 1 BEGIN TRY
 2     RAISERROR ('Error raised in TRY block.', -- Message text.
 3                 16, -- Severity.
 4                 1 -- State.
 5                );
 6 END TRY
 7 BEGIN CATCH
 8     DECLARE @ErrorMessage NVARCHAR(4000);
 9     DECLARE @ErrorSeverity INT;
10     DECLARE @ErrorState INT;
11 
12     SELECT 
13         @ErrorMessage = ERROR_MESSAGE(),
14         @ErrorSeverity = ERROR_SEVERITY(),
15         @ErrorState = ERROR_STATE();
16 
17     RAISERROR (@ErrorMessage,  -- Message text.
18                @ErrorSeverity, -- Severity.
19                @ErrorState     -- State.
20                );
21 END CATCH;

egg6:

--B. 在 sys.messages 中创建即席消息
以下示例显示如何引发 sys.messages 目录视图中存储的消息。
该消息通过 sp_addmessage 系统存储过程,以消息号50005添加到 sys.messages 目录视图中。

 1 sp_addmessage @msgnum = 50005,
 2                @severity = 10,
 3                @msgtext = N'<<%7.3s>>';
 4 GO
 5 
 6 RAISERROR (50005, -- Message id.
 7            10,    -- Severity,
 8            1,     -- State,
 9            N'abcde'); -- First argument supplies the string.
10 -- The message text returned is: <<    abc>>.
11 GO
12 
13 sp_dropmessage @msgnum = 50005;
14 GO

egg7:

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

 1 sp_addmessage @msgnum = 50005,
 2               @severity = 10,
 3               @msgtext = N'<<%7.3s>>';
 4 GO
 5 
 6 RAISERROR (50005, -- Message id.
 7            10,    -- Severity,
 8            1,     -- State,
 9            N'abcde'); -- First argument supplies the string.
10 -- The message text returned is: <<    abc>>.
11 GO
12 
13 sp_dropmessage @msgnum = 50005;
14 GO

偶然使用的,老大让我在他写好的框架的基础上做报表。ok,写好存储过程之后,发现错误不能以输出参数的方式显示出来,但是总不能不提示错误吧。so,查来这个RAISERROR,仅供参考。互相学习。

 

posted on 2012-10-08 10:23  yj_smile  阅读(388)  评论(0编辑  收藏  举报