Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2005
    Posts
    92

    Unanswered: sending mail from stored procedure?

    Dear All,

    I need to create a functionality in SQL server 2000 that would send out data from a table to users within our company.

    Is there some built in funcitonality wherby i can mail out the selected data from the table from a stored procedure? Then i could schedule the stored procedure to be run every morning at 7 am.

    Never done this, can some one let me know if that would work and give me some pointers on what commands to use in the stored procedure? Thanks in advance!

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    xp_sendmail is built in, but you need an exchange server and your sql service needs to be running as a domain account with an exchange account.

    xp_smtpmail you need to download and install and it requires access to a running smtp service from within IIS.

    and then there is CDOSYS, CDONTS...
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Mar 2005
    Posts
    92
    Thanks for that, I got an exchnage server on a seperate box.

    How do I make the sql service run as a domain account with exchnage? (sorry don't know much about this)

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Go to the Services screen in adminitirative tools in your control Panel, right click the MSSQLServer service and to the Log On tab. You do not want Local System Account. If there is a network account selected check with the guy who is in charge of the exchange box about getting that network account an exchange account. There is a little more to do this then that, but do yourself a favor and go to books online and read up on SQLMail and xp_sendmail.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    I use this with excellent results:

    http://www.sqldev.net/xp/xpsmtp.htm
    Paul

  6. #6
    Join Date
    Mar 2005
    Posts
    92
    Thanks for suggestions, will look into them now!

  7. #7
    Join Date
    Mar 2005
    Posts
    92
    The plug in works fine, so thanks for that again.

    I have a tiny problem though, I'm trying to assamble the body of the e-mail message like this:

    SET @myMESSAGE = (SELECT compName FROM @tblActionItems WHERE arrId_actionItems = @intLoop_ActionItems) + ' - ' + (SELECT projectName FROM @tblActionItems WHERE arrId_actionItems = @intLoop_ActionItems) + ' - ' + (SELECT tdNote FROM @tblActionItems WHERE arrId_actionItems = @intLoop_ActionItems) + ' - ' + CAST( (SELECT tdDateDue FROM @tblActionItems WHERE arrId_actionItems = @intLoop_ActionItems) as varchar(25))


    This being in a loop, I'm trying to fill the @myMESSAGE variable to contain all the items from the @tblActionItems

    I tried @myMESSAGE = @myMESSAGE + ' ' + ...

    but the e-mail this way does not appear to have a body.

    If I only input a single line as above, it works fine.

    Any suggestions?

  8. #8
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    A brief test suggests that @myMESSAGE starts out Null, and that's propagating through your string. Try adding

    Set @myMESSAGE = ' '

    before the loop.
    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •