I'm getting error while calling one statement through trigger I want to run trigger fine even if it has error inside it. so I used try and catch for that.
but even applying rollback and try/catch getting this error
Msg 3609, Level 16, State 1, Line 28 The transaction ended in the trigger. The batch has been aborted.
create table dbo.trigger_log(
id int,
message varchar(50),
lodadatetime datetime2
)
create table dbo.trigger_test (
id int,
name varchar(50),
status varchar(50)
)
create or alter TRIGGER [dbo].[trigger_error]
ON dbo.trigger_test
AFTER INSERT, UPDATE
AS
BEGIN
set xact_abort off
begin try
-- some insert/update operation
insert into dbo2.trigger_log(id, message, loaddatetime)
select 1, 'sdfsd',getdate()
insert into dbo2.trigger_log(id, message, loaddatetime)
select 1, 'sdfsd','df'
end try
begin catch
rollback
select 1
end catch
end
insert into dbo.trigger_test
select 1,'xyz','ok'
select * from dbo2.trigger_log
select * from dbo.trigger_test
Any Solution for this?
Is there a way that my trigger run successfully and throw error. I just want in case of failure inside trigger. Insertion/Update we are performing on main table shouldn't be affected.
2条答案
按热度按时间uz75evzq1#
If you rollback in a trigger then everything is rolled back, and you should never do this. If you actually wanted a rollback then you can simply remove the
SET XACT_ABORT OFF
as well as theTRY CATCH
and it will get rolled back.In this case, you want to swallow the error, so you need to remove the
ROLLBACK
.Do not
select
data out of a trigger, it's not properly supported.TRY
with an emptyCATCH
is an odd thing to do anyway. You should probably consider why you are getting errors and try to avoid them.vs91vp4v2#
In my opinion the trigger you have is overly complicated and flawed.
Firstly I suggest against using
ROLLBACK
in theTRIGGER
. For the above there is no need for it, as when theINSERT
statement fails the error generated will be propagated to the outer scope, and that will present the error to the front end. As the trigger failed, then the transaction will also be "automagically" rolled back.If you need to add logic in your trigger so that in the event of some logic you want the DML statement to fail then
THROW
a meaningful error, don'tROLLBACK
; this gives the front end a meaningful error and the transaction is still rolled back.You also have a
SELECT 1
in your trigger which isn't part of a assignment/DML operation. Returning a dataset from a trigger is deprecated and should be avoided. I don't know what the goal of theSELECT
in the above is, it justSELECT
s theint
value1
, so I assume it should simply not be there.This end up with your DDL statements just being the following:
If you then try to
INSERT
into the tabledbo.trigger_test
like you did, the following error now occurs:Msg 241, Level 16, State 1, Procedure trigger_error, Line 6
Conversion failed when converting date and/or time from character string.
In truth, "swallowing" an error is just not a good idea in any scenario. If the trigger errors, then there's a problem and it should be addressed. In this case, that's because of the incorrect data attempted to be inserted into the table
dbo.trigger_log
. That should be addressed. For a log table, inserting data into the "main" table and not doing the appropriate logging could be a significant concern so you need to be aware.