use*_*490 43 sql database sql-server-2005 sql-server-2008 sql-server-2008-r2
我刚刚创建了一个After After Trigger,其语法如下:
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
Run Code Online (Sandbox Code Playgroud)
在我使用的上述语法中 RAISERROR('Cannot Insert where salary > 1000',16,1)
但是当我写RAISERROR('Cannot Insert where salary > 1000')它时,会在同一行上给出错误"语法不正确").
任何人都可以在这里解释(16,1)的使用.
Dar*_*ren 61
它是错误的严重性级别.级别从11到20,这会在SQL中引发错误.级别越高,级别越严重,交易就应该中止.
当你这样做时,你会得到语法错误:
RAISERROR('Cannot Insert where salary > 1000').
Run Code Online (Sandbox Code Playgroud)
因为您尚未指定正确的参数(严重性级别或状态).
如果您希望发出警告而不是例外,请使用级别0 - 10.
来自MSDN:
严重
用户定义的严重性级别是否与此消息相关联.使用msg_id引发使用sp_addmessage创建的用户定义消息时,RAISERROR上指定的严重性将覆盖sp_addmessage中指定的严重性.任何用户都可以指定从0到18的严重级别.从19到25的严重级别只能由sysadmin固定服务器角色的成员或具有ALTER TRACE权限的用户指定.对于从19到25的严重性级别,需要WITH LOG选项.
州
是0到255之间的整数.负值或大于255的值会生成错误.如果在多个位置引发相同的用户定义错误,则为每个位置使用唯一的状态编号可帮助查找哪个代码段引发错误.
http://support.microsoft.com/kb/321903
har*_*rsh 28
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;
Run Code Online (Sandbox Code Playgroud)
您可以关注并尝试更多示例来自http://msdn.microsoft.com/en-us/library/ms178592.aspx
Woo*_*Moo 12
根据MSDN
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
Run Code Online (Sandbox Code Playgroud)
16将是严重性.
1将是国家.
您得到的错误是因为您没有正确提供该RAISEERROR功能所需的参数.
作为从Microsoft 的 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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
165319 次 |
| 最近记录: |