SQL Server Triggers - grouping by transactions

后端 未结 3 1529
南方客
南方客 2021-01-15 02:33

At work we have just started building an auditing framework for our database (i.e. to log what data has changed when it is created or updated).

We\'d quite like to i

相关标签:
3条回答
  • 2021-01-15 03:02

    Found this on the net to get the current transaction id (which you could then use to generate a batch id), but not sure if it will work or not:

    SELECT TOP 1
    @transactionID = req_transactionID
    FROM
      master..syslockinfo l
    INNER JOIN
      master..sysprocesses p ON l.req_spid = p.spid AND l.rsc_dbid = p.dbid AND p.spid = @@spid
    WHERE
     l.rsc_dbid = db_id() AND p.open_tran != 0 AND req_transactionID > 0
     ORDER BY req_transactionID 
    
    0 讨论(0)
  • 2021-01-15 03:04

    you can just use select * from sys.dm_tran_current_transaction

    sys.dm_tran_current_transaction

    0 讨论(0)
  • 2021-01-15 03:18

    Outside the scope of the question but something to think about when designing your audit solution. If you intend to audit records that include bulk inserts, make sure your bulk inserts all include the FIRE_TRIGGERS keywords. You also need to ensure that the triggers themselves properly handle multiple row inserts (and not, not, not through a cursor!).

    0 讨论(0)
提交回复
热议问题