MS SQL trigger - sending an email upon updating a table

Discussion in 'Databases' started by Ness, Nov 1, 2005.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello,</o:p>
    I am trying to execute an email when a MS SQL table has been updated, inserted, or deleted. I am trying to do this with a trigger using SQL mail. Here's more info.. </o:p>
    </o:p>
    My trigger.. </o:p>
    CREATE TRIGGER [CUSTOMER-TRIGGER] ON [xxx].[xxxxx]
    FOR INSERT, UPDATE, DELETE
    AS
    EXEC xp_sendmail 'robertk', 'The master database is full.'</o:p>
    </o:p>
    Error Message:</o:p>
    It doesn't work... I get the following error message</o:p>
    Exception Details: System.Data.SqlClient.SqlException: Could not find stored procedure 'xp_sendmail'. The statement has been terminated.</o:p>
    </o:p>
    Here's the Microsoft link I am using</o:p>
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/howtosql/ht_3_sqlmal_6gq4.asp </o:p>
    </o:p>
    More info on Microsoft website:</o:p>
    How to use SQL Mail (Transact-SQL)</o:p>
    <v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id=_x0000_i1025 style="WIDTH: 13.5pt; HEIGHT: 16.5pt" alt="" type="#_x0000_t75"><v:imagedata o:href="http://msdn.microsoft.com/library/en-us/howtosql/basics/update_topic.gif" src="file:///C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"></v:imagedata></v:shape>New Information - SQL Server 2000 SP3.</o:p>
    SQL Mail uses several extended stored procedures that are necessary for mail enabling. These extended stored procedures are included in a dynamic-link library, SQLMAP70.DLL, which is installed with Microsoft® SQL Server? 2000.</o:p>
    For security reasons, you should limit permissions for all SQL Mail stored procedures and extended stored procedures to members of the sysadmin fixed server role.</o:p>
    </o:p>
    </o:p>
    <A name=_MailAutoSig>Later,
    </A><st1:personName w:st="on"><u1:personName u2:st="on">Gene Ness
    </u1:personName>[email protected]</o:p>
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    xp_sendmail is not supported. We do not want to turn our SQL server into a mass mailing server!!

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Hello,</o:p>
    </o:p>
    No support of SQL mail means no trigger emails. It may cause us to reconsider.</o:p>
    </o:p>
    Do you support ... "SmtpMail.SmtpServer" ... If so do you have examples?</o:p>
    </o:p>
    Do you have any examplesofautomatically sending an email after a SQL table has be modified?</o:p>
    </o:p>
    Later,</o:p>
    <st1:personName w:st="on">Gene Ness</o:p>
    [email protected]</o:p>
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    do you mean system.web.mail class?

    In general, we do not allow sending email from our SQL server. You can create an application on hte web server to send mail.

    disabling xp_sendmail is rather standard in shared SQL hosting. if you find a host that allows you to do this, you probably want to avoid them, simply because they have no clue what they are doing.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  5. Hello,</o:p>
    Yes, I mean System.Web.Mail... Do you support this mail service? What mail service do you recommend?</o:p>
    The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. The classes in this namespace can be used from ASP.NET or from any managed application.</o:p>
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebmail.asp </o:p>
    </o:p>
    All I want to do is send an email notification if a SQL table is modified. Do you have any sample code for this?
    Gene Ness

    </o:p>
     
  6. Bruce

    Bruce DiscountASP.NET Staff

    system.web.mail is available on the web server.

    The problem is how would the web server know when to trigger the email. I doubt that you can do that on the web server.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  7. Hello Bruce,</o:p>
    </o:p>
    Thanks to your support team, I was able tosend an automatic email upon a visitor completing a form. I used "system.web.mail". I have the code if anyone wants it. One of the problems I had was I was trying to execute my Web Matrix email code on the Web Matrix server. Once I moved it to the DASP servers (and help for your staff) the code seems to be working fine.
    </o:p>
    However, it would be cleaner if I could do the same on SQL side. I understand your concern about a mass email system if you allowed SQL Mail. Is there anything I could do with stored procedures; ie, have the SQL Trigger call an aspx.page on the web server? Is there a way for SQL to call the web server? Any help/ideas would be of great assistance to us.

    Gene Ness
     
  8. Bruce

    Bruce DiscountASP.NET Staff

    Nope.. not that i can think of.

    The SQL server is located in a protected network, ie. we limit it from making connection to most port to the external world.

    sorry man.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  9. Hey Bruce, can the transaction logs be accessed programatically? If so, he may want to try a scheduled task that calls an ASP page that reads the log and emails updates/changes to him. Not sure if this as doable.






    Mike Reilly, Secretary/Webmaster
    Kiwanis Club of Rancho Penasquitos
    "Serving the Children of the World"
    [email protected]
    www.KiwanisPQ.org
    (760) 419-7429
     
  10. Bruce

    Bruce DiscountASP.NET Staff

    Do yo umean physically access the log? or through transact SQL.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page