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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 xp_sendmail in trigger

Author  Topic 

checkout
Starting Member

2 Posts

Posted - 2010-11-16 : 10:24:30
Hello world,
I'am trying to make a trigger that has to call a stored procedure when a tabel is updated.

The nect example is working without any problems:
EXEC master.dbo.xp_sendmail
@recipients = N'test@test.nl',
@query = N'SELECT * FROM INFORMATION_SCHEMA.TABLES',
@subject = N'SQL Server Report',
@message = N'The contents of INFORMATION_SCHEMA.TABLES:',
@attach_results = 'TRUE',
@width = 250 ;

I have made the following stored procedure. This has to send a email:

CREATE PROCEDURE [dbo].[send_mail] AS
exec master.dbo.xp_sendmail
@recipients='test@test.nl',
@subject='test'
GO

This i wanted to call from the following trigger:

CREATE TRIGGER [XML_trigger] ON [dbo].[XMLEvents]
FOR INSERT
AS
EXEC send_mail

But it is giving me the following error from SQL:

[Microsoft][ODBC SQL Server Driver][SQL Server]EXECUTE permission denied on opbject 'xp_sendmail', database 'master', owner 'dbo',

SQL State:42000
DB error:229

I can not figure out where this is going wrong. Because teh SP (xp_sendmail) is working fine from the Query Analyzer.

Please help to find out the solluition...

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-16 : 12:58:44
You should NOT send an email from a trigger. If SQL Mail hands, the trigger will hang, and the transaction will hang.

You should instead write a wrote to a table, and then have a SQL job in place to read the table every minute and send email if necessary.

Grant the user exec on xp_sendmail, the user that is running the insert.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -