how to create a trigger to send an email

  • Hi All

    Could you please help me with a insert trigger.

    All it has to perform is to send an email to an email address, when a new record is inserted to the table.  I think it should use master..xp_sendmail.

    Thanks

     

     

     

  • Try this:

    CREATE TRIGGER trSendEmail ON dbo.TableName FOR INSERT

    AS

    EXEC master.dbo.xpsendmail @Recipients = 'myemail@mycompany.com', @Subject = 'Test'

     

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • Hi Robert

    Thanks very much, it worked like a treat.

    Dinesh

  • Just a caution regarding this solution... see

    http://www.sqlservercentral.com/columnists/sjones/worstpracticetriggeringexternalevents.asp

    for how triggering external events can sometimes be a bad idea.

  • Yep becareful if you are doing this on a heavy used table it may get really bad. Try to use a separed table where you from the trigger store the changes and a separated process to handle the emails!

     


    * Noel

  • I just read that article.  Yes be sure you can accept the situations as explained in the article.  Where I used it was on an isolated table that was used only by DBA's.

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply