玉宇寒 发表于 2023-9-14 12:19:23

RAISERROR()语法意义是什么?

我刚创造了一个After After Trigger,语法如下:
Create trigger tgrInsteadTrigger on copytabletoInstead 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 ibegin    if (@sales >    begin      RAISERROR('Cannot Insert where salary > ROLLBACK;      end    else      begin      insert into copytablefrom(store_name,sales,date) values (@store_name,@sales,@date);      Print 'Instead After Trigger Executed      endEnd在上述语法中,我使用了 RAISERROR('Cannot Insert where salary > 1000',16,1)
但是当我写RAISERROR('Cannot Insert where salary >1000')它在同一行上犯了错误’)’附近的语法不正确。
任何人都可以在这里解释(16、1)的用法。
                                                               
    解决方案:                                                               
                                                                是严重程度error。级别是11-会导致错误SQL。级别越高,级别越严重,transaction该级别应暂停。
在执行以下操作时,您将收到语法错误:
RAISERROR('Cannot Insert where salary > 1000').因为你没有指定正确的指定parameters(severity level或state)。
如果你想发出警告,而不是警告exception,请使用0-10级。
从MSDN:
严重程度
用户定义的严重性水平与消息相关。msg_id引发使用sp_addmessage在创建用户定义的消息时RAISERROR上述指定的严重性将被覆盖sp_addmessage中指定的严重性。任何用户都可以指定从0到18的严重级别。只能由sysadmin具有固定服务器角色的成员或ALTER
TRACE权限用户指定从19到25的严重程度。对于19到25的严重程度,需要WITH LOG选项。
状态
是0到255之间的整数。负值或大于255的值会产生错误。如果同一用户定义错误发生在多个位置,使用每个位置的唯一状态号可以帮助找到导致错误的代码部分。这里有一个详细的解释
页: [1]
查看完整版本: RAISERROR()语法意义是什么?