I have SQL Server 2005 stored procedure. Someone one is calling my stored procedure within a transaction. In my stored proc I’m logging some information (insert into a table). When the higher level transaction rolls back it removes my insert.
Is there anyway I can commit my insert and prevent the higher level rollback from removing my insert?
Thanks
Even if you start a new transaction, it will be nested within the outer transaction. SQL Server guarantees that a rollback will result in an unmodified database state. So there is no way you can insert a row inside an aborted transaction.
Here’s a way around it, it’s a bit of a trick. Create a linked server with
rpc out = trueandremote proc transaction promotion = false. The linked server can point to the same server as your procedure is running on. Then, you can useexecte (<query>) at <server>to execute something in a new transaction.This will end with a row in the log table, even though the transaction was rolled back.
Here’s example code to create such a linked server: