【问题标题】:What is the syntax meaning of RAISERROR()RAISERROR() 的语法含义是什么
【发布时间】:2013-04-23 13:02:41
【问题描述】:

我刚刚创建了一个相反的触发器,其语法如下:

Create trigger tgrInsteadTrigger on copytableto
Instead of Insert as 
    Declare @store_name varchar(30);
    declare @sales int;
    declare @date datetime;

    select @store_name = i.store_name from inserted i
    select @sales = i.sales from inserted i
    select @date = i.Date from inserted i
begin
    if (@sales > 1000)
        begin
        RAISERROR('Cannot Insert where salary > 1000',16,1); ROLLBACK;
        end
    else
        begin
        insert into copytablefrom(store_name, sales, date) values (@store_name, @sales, @date);
        Print 'Instead After Trigger Executed';
        end
End

在上面的语法中我使用了RAISERROR('Cannot Insert where salary > 1000',16,1)

但是当我写RAISERROR('Cannot Insert where salary > 1000') 时,它在同一行给出了错误“')' 附近的语法错误”。

谁能在这里解释一下 (16,1) 的用法。

【问题讨论】:

标签: sql sql-server database sql-server-2008 sql-server-2008-r2


【解决方案1】:

这是error 的严重级别。级别为 11 - 20,这会在 SQL 中引发错误。级别越高,级别越严重,transaction 应该被中止。

这样做时会出现语法错误:

RAISERROR('Cannot Insert where salary > 1000').

因为您没有指定正确的parametersseverity levelstate)。

如果您希望发出警告而不是exception,请使用级别 0 - 10。

来自 MSDN:

严重性

是与此消息关联的用户定义的严重级别。什么时候 使用 msg_id 引发使用创建的用户定义消息 sp_addmessage,在 RAISERROR 上指定的严重性会覆盖 sp_addmessage 中指定的严重性。严重级别从 0 到 18 可以由任何用户指定。从 19 到 25 的严重级别可以 只能由 sysadmin 固定服务器角色的成员指定,或 具有 ALTER TRACE 权限的用户。对于从 19 起的严重级别 到 25,需要 WITH LOG 选项。

状态

是从 0 到 255 的整数。负值或值 大于 255 会产生错误。如果相同的用户定义错误是 在多个地点提出,每个地点使用唯一的州编号 location 可以帮助找到哪个代码段引发了错误。 详细说明here

【讨论】:

  • 谢谢,你的回答清楚了我的概念,但你能否解释一下状态背后的概念,RAISERROR() 的第三个参数。
  • @user2289490 - 它可以简单地用于确定错误被抛出的位置。 IE。如果您在状态 1 中引发错误,然后引发另一个错误(在存储过程的不同部分),您可以跟踪过程的哪个部分引发了异常。
【解决方案2】:

16 是严重性,1 是状态,更具体地说,以下示例可能会为您提供有关语法和用法的更多详细信息:

BEGIN TRY
    -- RAISERROR with severity 11-19 will cause execution to 
    -- jump to the CATCH block.
    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();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;

您可以关注并尝试来自http://msdn.microsoft.com/en-us/library/ms178592.aspx的更多示例

【讨论】:

    【解决方案3】:

    根据MSDN

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

    16 是严重性。
    1 是状态。

    您得到的错误是因为您没有为RAISEERROR 函数正确提供所需的参数。

    【讨论】:

      【解决方案4】:

      示例代码中的严重级别 16 通常用于用户定义的(用户检测到的)错误。 SQL Server DBMS 本身会针对它检测到的问题发出severity levels(和错误消息),无论是更严重的问题(较高的数字)还是不太严重的问题(较低的数字)。

      状态应该是 0 到 255 之间的整数(负值会出错),但选择基本上是程序员的。如果用户定义错误的相同错误消息将在不同位置引发,则放置不同的状态值很有用,例如如果通过额外指示错误发生的位置来帮助调试/排除问题。

      【讨论】:

        【解决方案5】:

        作为来自Microsoft's MSDN 的示例发布到这个问题的答案很好,但是如果它不是来自 TRY 块,它并不能直接说明错误来自哪里。我更喜欢这个示例,它对 CATCH 块中的 RAISERROR 消息进行了非常小的更新,说明错误来自 CATCH 块。我也在 gif 中演示了这一点。

        BEGIN TRY
            /* RAISERROR with severity 11-19 will cause execution
             |  to jump to the CATCH block.
            */
            RAISERROR ('Error raised in TRY block.', -- Message text.
                       5, -- Severity. /* Severity Levels Less Than 11 do not jump to the CATCH block */
                       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();
        
            /* Use RAISERROR inside the CATCH block to return error
             | information about the original error that caused
             | execution to jump to the CATCH block
            */
            RAISERROR ('Caught Error in Catch', --@ErrorMessage, /* Message text */
                       @ErrorSeverity,                           /* Severity     */
                       @ErrorState                               /* State        */
                       );
        END CATCH;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-11
          • 2022-07-07
          • 2014-08-20
          • 1970-01-01
          • 2018-01-22
          • 2014-09-12
          相关资源
          最近更新 更多