SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why is SQL so backward FFS?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
172 Posts

Posted - 10/10/2013 :  15:17:24  Show Profile  Reply with Quote
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

USA
37466 Posts

Posted - 10/10/2013 :  15:46:33  Show Profile  Visit tkizer's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/10/2013 :  15:54:47  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000