adimit
asked on
sql server transactions not committing within an instead of insert trigger
I have some code in an instead of trigger(on a table) that performs the following
begin try
declare@ .........
set @erred = 0
begin transaction @t1
set @erred = 0
insert into table x from inserted ......
commit transaction@t1
set @erred =0
end try
begin catch
if @erred = 1 begin	rollback transaction @t1 END
	DECLARE @TIME AS DATETIME
	SET @TIME = GETDATE()
	EXEC USP_GETERRORINFO @TIME
end catch
This transaction is placed on tables 1,2,3,4 and 5
I execute the transaction on table 1 by inserting valid data into table 1. The trigger successfully sends data into table x.
I execute the transaction on table 2 by inserting invalid data into table 2, thus forcing an error. the trigger doesn't add any new data, but the data inserted by the successful transaction in table 1also gets deleted.
Why is that? I.e why does the 'old' data get deleted as well.Does the transaction stay open for some reason all the time. The text for @t1 is different in each trigger. I can guarantee that there are no delete statements in the code
begin try
declare@ .........
set @erred = 0
begin transaction @t1
set @erred = 0
insert into table x from inserted ......
commit transaction@t1
set @erred =0
end try
begin catch
if @erred = 1 begin	rollback transaction @t1 END
	DECLARE @TIME AS DATETIME
	SET @TIME = GETDATE()
	EXEC USP_GETERRORINFO @TIME
end catch
This transaction is placed on tables 1,2,3,4 and 5
I execute the transaction on table 1 by inserting valid data into table 1. The trigger successfully sends data into table x.
I execute the transaction on table 2 by inserting invalid data into table 2, thus forcing an error. the trigger doesn't add any new data, but the data inserted by the successful transaction in table 1also gets deleted.
Why is that? I.e why does the 'old' data get deleted as well.Does the transaction stay open for some reason all the time. The text for @t1 is different in each trigger. I can guarantee that there are no delete statements in the code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER