My question is about abstract software engineer point of view :
Suppose I want to send an email after each insertion of row to a table.
Which approach will be smarter and “by the book” ?
- create a C#/CLR assembly which will send mail
- enable the CLR in SQL Server
- create trigger after insert, using that assembly’s function to send mail
or
- in C# application program itself , after the insertion to DB – send the email.
Use Database Mail, which is a built in emailing solution for SQL Server, if you’re going to do the emailing from the server (rather than writing your own solution).
This, at least, ensures that delivery of mail is asynchronous with respect to the rest of your transaction, so would be safe to call from a trigger or stored proc, without breaking either of these if the email delivery takes a while to succeed.
As to, now, the decision of whether you should use this, called from a trigger or stored proc, or do the work in the application, it depends on which you’re more comfortable with. In either case, you need to consider the various failure conditions rather than just the happy path. If the SQL portion succeeds, and then the application crashes (or is killed through task manager) before it sends the email, how serious a failure would that be? What mitigating steps would you have to put in place?