Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Triggers in SQL Server 2005/2008

I have triggers that manipulate and insert a lot of data into a Change tracking table for audit purposes on every insert, update and delete.

This trigger does its job very well, by using it we are able to log the desired oldvalues/newvalues as per the business requirements for every transaction.

However in some cases where the source table has a lot columns, it can take up to 30 seconds for the transaction to complete which is unacceptable.

Is there a way to make the trigger run asynchronously? Any examples.

like image 269
Jose Basilio Avatar asked Apr 20 '09 03:04

Jose Basilio


People also ask

Are SQL Server trigger asynchronous?

You can't make the trigger run asynchronously, but you could have the trigger synchronously send a message to a SQL Service Broker queue. The queue can then be processed asynchronously by a stored procedure.

Are SQL triggers synchronous or asynchronous?

The trigger call is not asynchronous. Each call to your insert procedure will result in the trigger being fired, and the procedure will not return until the trigger finishes.

What are 3 types of SQL triggers?

SQL Server has three types of triggers: DML (Data Manipulation Language) Triggers. DDL (Data Definition Language) Triggers. Logon Triggers.

Are triggers asynchronous?

Triggers are run synchronously by default. Asynchronous triggers run in the background, independent of other operations that follow. They are typically run after an event completes.


2 Answers

these articles show how to use service broker for async auditing and should be useful:

Centralized Asynchronous Auditing with Service Broker

Service Broker goodies: Cross Server Many to One (One to Many) scenario and How to troubleshoot it

like image 20
Mladen Prajdic Avatar answered Sep 21 '22 13:09

Mladen Prajdic


You can't make the trigger run asynchronously, but you could have the trigger synchronously send a message to a SQL Service Broker queue. The queue can then be processed asynchronously by a stored procedure.

like image 142
Sean Reilly Avatar answered Sep 22 '22 13:09

Sean Reilly