Logging into table in SQL Server trigger

前端 未结 4 475
猫巷女王i
猫巷女王i 2021-01-13 14:30

I am coding SQL Server 2005 trigger. I want to make some logging during trigger execution, using INSERT statement into my log table. When there occurs error during execution

4条回答
  •  一个人的身影
    2021-01-13 15:30

    The problem here is that logging is part of transaction that modifies your data. Nested transactions will not help here. What you need is to put you logging actions into a separate context (connection), i.e. make it independent from you current transaction.

    Two options come to my mind:

    • use Service Broker for logging - put log data to queue, receive and save the data 'on the other side of the pipe' (i.e. in another process/connection/transaction)
    • use OPENQUERY - you will need to register you own server as a 'linked server' and execute queries 'remotely' (I know, this looks a little bit strange, but an option anyway ...)

    HTH

提交回复
热议问题