0

We currently have a trigger set up on a table to audit any data changes similar to this answer, which uses dynamic SQL to generate the audit rows. In order to use the INSERTED and DELETED tables in the dynamic SQL they have to be inserted into temporary tables.

We are currently trying to limit our tempdb usage as it has been causing issues and this table is udpated frequently, and is often causing tempdb contention as the trigger is creating and dropping so many temporary tables.

Is there a way to audit data changes to this table without using temporary tables? Obviously I can hard code in the columns myself into the trigger and audit tables, but I'd rather have a solution which doesn't need updating every time there is a schema change on the table.

I have been looking around and I expect the answer to be 'No you can't do this' but I thought it was worth an ask in case I was missing some obscure method (or newer feature as we are on SQL Server 2016). As you can guess the solution needs to have good performance, otherwise it will cause issues somewhere else!

1
  • 2
    Would be better to code gen the triggers at development time rather than using dynamic SQL at runtime Commented May 1, 2020 at 11:29

2 Answers 2

2

Obviously I can hard code in the columns myself into the trigger and audit tables, but I'd rather have a solution which doesn't need updating every time there is a schema change on the table.

That is nearly the correct approach. Write a stored procedure to generate your triggers and run it any time you change the schema.

0

Please check if Change Data Capture meet your requirments.

The may advantage is you can easily config it and all the changes to your table(s) will be tracked. The tracking process is done asynchronous so it will add minimal overhead to your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.