您的位置:首页 > 数据库 > 数据库管理 > 正文

SQL Server中raiserror

更多 时间:2015-3-8 类别:数据库 浏览量:2068

SQL Server中raiserror

SQL Server中raiserror

一、raiserror语法

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

    参数说明

    第一个参数:{ msg_id | msg_str | @local_variable }

    msg_id消息代号,用户定义错误消息的错误号应当大于 50000。
    msg_str:用户定义的错误消息,该错误消息最长可以有 2047 个字符;当指定 msg_str 时,RAISERROR 将引发一个错误号为 5000 的错误消息。

    @local_variable:按照 msg_str 方式的格式化字符串变量。

     

    第二个参数:severity

    用户定义的与该消息关联的严重级别。任何用户都可以指定 0 到 18 之间的严重级别。
     

    1、[0,10]的闭区间内,不会跳到catch;
    2、如果是[11,19],则跳到catch;
    3、如果[20,无穷),则直接终止数据库连接;

     

    第三个参数:state

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

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

    第四个参数:argument

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

    第五个参数:option

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

    LOG :在错误日志和应用程序日志中记录错误;
    NOWAIT:将消息立即发送给客户端;
    SETERROR:将 @@ERROR 值和 ERROR_NUMBER 值设置为 msg_id 或 50000

     

    二、SQL Server中raiserror实例

     

    1、

  •  
  • SQL 代码   复制
  • 
    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、

  •  
  • SQL 代码   复制
  • 
    RAISERROR (
                 N'This is message %s %d.', -- Message text,
                 10,                        -- Severity,
                 1,                         -- State,
                 N'number',                 -- First argument.
                             -- Second argument.
              ); 
    -- The message text returned is: This is message number 5.
    GO
    
    		
  •  

    3、

  •  
  • SQL 代码   复制
  • 
    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、从 CATCH 块返回错误消息

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

  •  
  • SQL 代码   复制
  • 
    BEGIN TRY
        RAISERROR ('Error raised in TRY block.', -- Message text.
                    16, -- Severity.
    -- 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、

  •  
  • SQL 代码   复制
  • 
    
    IF EXISTS (SELECT * FROM SYSOBJECTS WHERE name='my_sp_test' AND TYPE='P') BEGIN
    
        DROP PROCEDURE my_sp_test;
    
    END;
    
    GO
    
    create procedure my_sp_test @i int, @outstr varchar(100) out as
    
    begin try
    
        declare @j int;
    
        if @i<10 begin
    
          set @outstr = 'system exception.';
    
          set @j = 10/0;  -- 因为被除数为0,所以这里将会抛出一个系统的异常
    
        end
    
        else begin
    
          set @j = @i;
    
          set @outstr = 'customer exception';
    
          -- 抛出自定义的异常,在最后的catch块中统一处理异常
    
          RAISERROR (66666, -- Message id.
    
               16, -- Severity,
    
               1 -- State,
    
               ) ;    
    
        end;
    
    end try
    
    begin catch 
    
        if @@ERROR=66666 begin  -- 通过@@ERROR的值来判断是否是自定义的异常
    
            set @outstr = @outstr  + '---------------- customer exception';
    
        end;
    
        return;
    
    end catch;
    
    go
    
    		
  • 执行该存储过程

  •  
  • SQL 代码   复制
  • 
    
    DECLARE @OUTSTR11 VARCHAR(100);
    
    exec dbo.my_sp_test 12,@OUTSTR11 out
    
    print @OUTSTR11;
    
    		
  •  

     

    标签:SQL Server
    您可能感兴趣