Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why is SQL so backward FFS?

Author  Topic 

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2013-10-10 : 15:17:24
I set a trigger up on a table so that I would know when a new record had been inserted via email.

Nothing complicated, it's as basic as that.

A new record is created, I receive an email.

The first time it kicks in it not only stops the user creating the new record but doesn't email me anything.

It just tells the end user "EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'" because, of course, they'll know what that means.

WTF?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-10 : 15:46:33
Why are you blaming SQL for this? That's how triggers work. Everything inside the trigger is part of the transaction. This is why you should never send an email inside a trigger. Instead you should write to a table and have a job in place to read that table. This way it is async and does not impact the transaction. Regarding the permission, well the user must have permissions to everything in the trigger. It's a valid error.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-10-10 : 15:54:47
Already composed this after Tara's response but I'll send it anyway - for another opinion :)

Don't blame sql server for your shortcomings

It is a bad design to tie user transactions to any external process - like mail. Maybe a better idea is to set up an audit table for this table. Then anytime you want you can see who inserted/updated/deleted any row and when they did it. If you want an email notification you can set up a job (outside the scope of user transactions) to read from the audit table and send whatever you need.

Triggers are a powerful tool but dangerous too because of the nature of transactions.
Permissions and security is another big topic. It is likely that you don't want whatever security context your users have to have permission to certain things (like sending mail from the server).

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -