回答

收藏

RAISERROR()语法意义是什么?

技术问答 技术问答 206 人阅读 | 0 人回复 | 2023-09-14

我刚创造了一个After After Trigger,语法如下:
* D% |: r- {# l  y% xCreate 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)* G+ Z# B: x! s" N: u0 N7 w$ B
但是当我写RAISERROR('Cannot Insert where salary >1000')它在同一行上犯了错误’)’附近的语法不正确。
6 D0 c# k7 }( x5 `任何人都可以在这里解释(16、1)的用法。4 w6 k8 K. t# I9 e& }
                                                               
. X% h7 q+ X8 S) S    解决方案:                                                               
) K+ A) X6 E( b3 s& t, y, i                                                                是严重程度error。级别是11-会导致错误SQL。级别越高,级别越严重,transaction该级别应暂停。; R/ C% }0 Q, W& e
在执行以下操作时,您将收到语法错误:% ]6 O) Y" p% I$ n& L) p; ^) a
RAISERROR('Cannot Insert where salary > 1000').因为你没有指定正确的指定parameters(severity level或state)。# v2 d. b$ P5 p0 d8 b6 c6 x
如果你想发出警告,而不是警告exception,请使用0-10级。
0 {1 P) \2 F# d& d* m6 l$ a从MSDN:
% |( P# V2 P# t9 j7 K2 |8 X严重程度8 \. L0 i+ m; b& f6 }8 l
用户定义的严重性水平与消息相关。msg_id引发使用sp_addmessage在创建用户定义的消息时RAISERROR上述指定的严重性将被覆盖sp_addmessage中指定的严重性。任何用户都可以指定从0到18的严重级别。只能由sysadmin具有固定服务器角色的成员或ALTER9 v7 }8 h) y8 U1 ]4 Y
TRACE权限用户指定从19到25的严重程度。对于19到25的严重程度,需要WITH LOG选项。
0 }2 c4 L7 K3 M, y1 o状态, s+ O0 x- Z# i8 @+ T7 E
是0到255之间的整数。负值或大于255的值会产生错误。如果同一用户定义错误发生在多个位置,使用每个位置的唯一状态号可以帮助找到导致错误的代码部分。这里有一个详细的解释
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则