Solved

how to send emails when records inserted?

Posted on 2002-03-06
3
254 Views
Last Modified: 2012-05-04
I like to send the email when the record in a table is inserted. the table will have the email address already.

should I use trigger in the table, never used it before, anyone provide some simple code to get me started.

yeah, i know, i will look into the sql help too, but i like to have people experience if i have problem understaning the sql help.

how to setup the email on the sql server as well.

Thanks
0
Comment
Question by:chrislee8
3 Comments
 
LVL 3

Accepted Solution

by:
meos earned 50 total points
ID: 6844922
you may use SQL built in SQL Mail with stored procedures: xp_sendmail; but also external utility (ie: sendmail.exe)

use triggers for table as:
--------------------------
CREATE TRIGGER reminder
ON titles
FOR INSERT
AS
   EXEC master..xp_sendmail 'MaryM',
      'Don''t forget to print a report for the distributors.'

--------------------------
or xp_cmdshell
--------------------------
CREATE TRIGGER reminder
ON titles
FOR INSERT
AS
   EXEC master..xp_cmdshell 'c:\sendmail.exe mary@hot.com "'Don''t forget to print a report for the distributors."'
--------------------------

hope it helps
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6846869
As meos indicates, using the xp_cmdshell inside the trigger is the "simple" solution.
However, this has some disadvantages:
* the insert statment will be delayed if any problems appear during the mail send
* the mail send cannot be tracked
* the mail send cannot easily be configured

Thus, i do not send the email directly, but insert a short record in a working table (like "TODO_SENDEMAIL_REMINDER"), with has only the ID fields to the related table, a timestamp field and a flag saying if the email has been send...
To send the emails, you can still use xp_cmdshell, by running a job for example, looping on that table. Of course, you can also use some other external process and mailing tools...

CHeers
0
 

Expert Comment

by:CleanupPing
ID: 9280792
chrislee8:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now