I am trying to develop a mail trigger. Could someone assist on how this could be achieved so that when a use inserts a record it check the “speed” field such that when the inserted value exceeds 100, a mail is send to the specified address.
Share
First you need to set up database mail – if you haven’t done so, this question might help:
Then you need a trigger:
Now, you’re probably going to say you want data from the insert to be actually be included in the e-mail. And your first inclination is going to be to declare some local variables and assign them from
inserted– this doesn’t work because your trigger could be responding to a multi-row insert. So the right way to do this is:That all said, I am not a big fan of sending e-mail from a trigger. Even though database mail uses service broker and so is asynchronous, I would be much more inclined to populate a queue table, and have a background thread that comes around and sends all of the appropriate e-mails. The
twothree nice things about this are:sp_send_dbmail– which I inadvertently suggested – prevented the insert).