question

ana avatar image
ana asked

How to create trigger for multiple inserts on a table?

I have a table which gets new rows inserted on a daily basis via a scheduled job. I want to send one email with all inserted records for a particular day using trigger. Could somebody help me in how to create a trigger for multiple inserts on a table?

triggerchange-tracking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered

So you only want to send one email, once the job that inserts the data is finished?

Capture the inserted records into another table (by trigger or stored proc, or whatever you want), then add another step to the job to simply email you, creating the body of the email with the records.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rob Farley avatar image
Rob Farley answered

Turn on Change Data Capture on the table, so that you can easily query for the inserted records in a stored procedure that you run (through a SQL Agent Job) once a day.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

TimothyAWiseman avatar image
TimothyAWiseman answered

Kev Riley's answer is a very good option.

Another option you can consider is to not use a trigger at all but instead add a timestamp column. You can then write a stored procedure to select, format, and e-mail all rows created within the past 24 hours and set that on an SQL Server Agent Job. I used a technique like this to send management daily reports before.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.