RAISERROR()的语法含义是什么 [英] What is the syntax meaning of RAISERROR()

查看:83
本文介绍了RAISERROR()的语法含义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了一个After After Trigger,其语法如下:

I just created a Instead After Trigger whose syntax is given below:

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('无法在薪水> 1000处插入',16,1)

In the above syntax I have used RAISERROR('Cannot Insert where salary > 1000',16,1)

但是当我写 RAISERROR('无法在薪水> 1000处插入'),在同一行上显示错误')'附近的错误语法。

But when I write RAISERROR('Cannot Insert where salary > 1000') it gives the error "Incorrect syntax near ')'" on the same line.

任何人都可以在这里解释(16,1)的用法。

Can anyone please explain the use of (16,1) here.

推荐答案

这是<$ c的严重性级别$ c>错误。级别为11-20,这会在 SQL 中引发错误。级别越高,级别越严重,应该放弃事务

It is the severity level of the error. The levels are from 11 - 20 which throw an error in SQL. The higher the level, the more severe the level and the transaction should be aborted.

执行此操作时,您将得到语法错误:

You will get the syntax error when you do:

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

因为您没有指定正确的参数严重级别状态)。

Because you have not specified the correct parameters (severity level or state).

如果您要发出警告而不是警告例外,使用级别0-10。

If you wish to issue a warning and not an exception, use levels 0 - 10.

来自MSDN:


严重性

severity

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

Is the user-defined severity level associated with this message. When using msg_id to raise a user-defined message created using sp_addmessage, the severity specified on RAISERROR overrides the severity specified in sp_addmessage. Severity levels from 0 through 18 can be specified by any user. Severity levels from 19 through 25 can only be specified by members of the sysadmin fixed server role or users with ALTER TRACE permissions. For severity levels from 19 through 25, the WITH LOG option is required.

state

是0到255之间的整数。负值或大于255的值
会产生错误。如果相同的用户定义错误是在多个位置引发的
,则对每个
位置使用唯一的状态编号可以帮助查找引起错误的代码部分。
有关详细说明,请此处

Is an integer from 0 through 255. Negative values or values larger than 255 generate an error. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors. For detailed description here

这篇关于RAISERROR()的语法含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆