Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 

Oak Ridge, TN, 37830
United States


Sending email using SQL

Wade Cantley

Email on a bEach

3 oz vodka
1 oz peach schnapps
4 oz cranberry juice
4 oz orange juice

Simply put, you get the mail profile, and use a MSSQL procedure to send it.

/* Dynamically generates the profile name based on the server */
SET @DBMailProfile = SUBSTRING(@@SERVERNAME,1,(CHARINDEX('\',@@SERVERNAME)-1)) + '_default'
SELECT @DBMailProfile

EXEC msdb.dbo.sp_send_dbmail
@profile_name = @DBMailProfile,
@recipients = '',
@body = 'The stored procedure finished successfully.',
@subject = 'Automated Success Message' ;

Here is an example where a query is passed.

-- if there is a return on the above select, insert the results and send an email showing the results.
    IF @@ROWCOUNT > 0

            -- Email the results if a new record appears.
            EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'JobStatus',
            @recipients = '',
            @body = 'There are new records in the table.',
            @subject = 'New record that requires yoru attention.',
            @query = '
                SELECT * from tblWithData
                where isNew = 1