Hi All,
Some times we have requirement to send an e-mail, if the query returns data. please use the below code:
==============================================
use DATABASE_NAME
go
if exists(SELECT DstDesc, DstDT, COUNT(*) as [Count] from Distributions where
DstDT > '25-Aug-2011' group by DstDesc, DstDT having COUNT(*) > 1)
begin
--print 'test'
exec msdb..sp_send_dbmail @profile_name = 'SQL_MAIL_Profile_NAME',
@recipients = 'user_name@mail.com',
@subject = 'Duplicate Distribution',
@body = 'Please find below the duplicate entries in Distributions table',
@query = 'set nocount on
SELECT DstDesc, DstDT, COUNT(*) as [Count] from Distributions where
DstDT > ''25-Aug-2011'' group by DstDesc, DstDT having COUNT(*) > 1
set nocount off',
@execute_query_database = 'DATABASE_NAME',
@query_result_header = 1,
@query_result_width = 256
end
==============================================
Thanks,
Satish Kumar.
No comments:
Post a Comment