I am using sql express 2008 r2 express edition.
Is it possible to do writes inside a trigger independent from the the transaction of the statement which causes the trigger to fire?
I have triggers working for simple auditing on some tables in my DB. I have one table for keeping the auditing data (Look here for details) It works fine except i don't want the auditing data be vanished when the transaction rolledback. So I want to write audit data to audit table independent from the transaction of the statement that causes my auditing trigger to fire.
Best Answer
One solution is to add a linked server to your own sever. On the linked server, disable transactions. Any calls made to the linked server will now run in their own transaction.
Example setup:
Test code:
The
select
will show only 1 row. Therollback
has not undone the effefcts of the linked server call.While a nice trick, this is probably not something you'd want to do inside a production environment. Personally I'd only allow data modifications through a stored procedure, and add logging in the stored procedure itself, before it starts a transaction (if any.)