如何处理触发器中的异常?
how to handle an exception in trigger?
我知道这个问题可能与this question重复。但是我并没有通过这个问题消除我的困惑。在SQL中,我们使用触发器来对数据库进行插入、更新、删除等操作queries.but我们如何处理After insert statement in trigger中发生的异常?例如..
CREATE TRIGGER triggerAfterInsert ON Employee
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit varchar(100);
select @empid=i.Emp_ID from inserted i;
select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;
set @audit='Inserted Record -- After Insert Trigger.';
insert into Employee_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate());
PRINT 'AFTER INSERT trigger executed.'
GO
在上面的插入查询中,当我将文本值而不是整数设置为 empid 时,即 @empid=a 然后它在触发器中抛出异常,我不知道如何处理这些异常,有人可以建议我解决这个问题吗?
示例:
创建时
CREATE TRIGGER tr_Ins_Test_Trig ON dbo.Test_Trig
AFTER INSERT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Test_Trig2
SELECT id + 1 FROM INSERTED
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO Trig_Error
SELECT *,'err' FROM inserted
END CATCH
END
GO
示例:
改变时
ALTER TRIGGER tr_Ins_Test_Trig ON dbo.Test_Trig
AFTER INSERT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Test_Trig2
SELECT id + 1 FROM INSERTED
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @Trig_Error TABLE(
id INT, col CHAR(3)
);
INSERT INTO @Trig_Error
SELECT *,'err' FROM inserted
ROLLBACK TRANSACTION
INSERT INTO Trig_Error
SELECT * FROM @Trig_Error
END CATCH
END
GO
他的办案更像是走流程
这是一个使用用户定义异常的异常处理示例:
DECLARE
v_cnt NUMBER;
count_more_than_one EXCEPTION;
BEGIN
select count(*) into v_cnt
from whatever
where whatever;
if v_cnt > 1 then
raise count_more_than_one;
else
whatever;
end if;
EXCEPTION
when count_more_than_one then
dbms_output.put_line('Some error message you can build here or up above');
when no_data_found then
this is a built-in exception;
END;
我知道这个问题可能与this question重复。但是我并没有通过这个问题消除我的困惑。在SQL中,我们使用触发器来对数据库进行插入、更新、删除等操作queries.but我们如何处理After insert statement in trigger中发生的异常?例如..
CREATE TRIGGER triggerAfterInsert ON Employee
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit varchar(100);
select @empid=i.Emp_ID from inserted i;
select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;
set @audit='Inserted Record -- After Insert Trigger.';
insert into Employee_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate());
PRINT 'AFTER INSERT trigger executed.'
GO
在上面的插入查询中,当我将文本值而不是整数设置为 empid 时,即 @empid=a 然后它在触发器中抛出异常,我不知道如何处理这些异常,有人可以建议我解决这个问题吗?
示例: 创建时
CREATE TRIGGER tr_Ins_Test_Trig ON dbo.Test_Trig
AFTER INSERT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Test_Trig2
SELECT id + 1 FROM INSERTED
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO Trig_Error
SELECT *,'err' FROM inserted
END CATCH
END
GO
示例: 改变时
ALTER TRIGGER tr_Ins_Test_Trig ON dbo.Test_Trig
AFTER INSERT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Test_Trig2
SELECT id + 1 FROM INSERTED
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @Trig_Error TABLE(
id INT, col CHAR(3)
);
INSERT INTO @Trig_Error
SELECT *,'err' FROM inserted
ROLLBACK TRANSACTION
INSERT INTO Trig_Error
SELECT * FROM @Trig_Error
END CATCH
END
GO
他的办案更像是走流程
这是一个使用用户定义异常的异常处理示例:
DECLARE
v_cnt NUMBER;
count_more_than_one EXCEPTION;
BEGIN
select count(*) into v_cnt
from whatever
where whatever;
if v_cnt > 1 then
raise count_more_than_one;
else
whatever;
end if;
EXCEPTION
when count_more_than_one then
dbms_output.put_line('Some error message you can build here or up above');
when no_data_found then
this is a built-in exception;
END;