Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Send Mail with Query Results

Author  Topic 

sz1
Aged Yak Warrior

555 Posts

Posted - 2013-10-22 : 06:53:32
Hi,

Is it possible to send the query results with an email command? rather than setup a Stored Procedure?

Thanks

Use Test
GO


SELECT
p.[ProblemNumber],
COUNT(i.[IncidentNumber]) as ActiveIncidents

FROM [Problem] p

JOIN [Incident] i
ON P.[RecID] = i.[ProblemRecID]


WHERE p.[Status] <> 'Closed'
AND i.[Status] = 'Active'

GROUP BY p.[ProblemNumber]

Use msdb
Go

EXEC sp_send_dbmail @profile_name='ITServices',
@recipients='itservices@mycomp.co.uk',
@subject='Active Incidents',
@body='Active Incidents Attached to Problems.',
@importance = 'High'

SZ1
Please help me to enable me to help others!

sz1
Aged Yak Warrior

555 Posts

Posted - 2013-10-22 : 07:25:29
Sorted it with this script.

EXEC sp_send_dbmail
@profile_name='ITServices',
@recipients='itservices@mycomp.co.uk',
@subject='Active Incidents',
@body='Active Incidents Attached to Problems.',
@importance = 'High',
@query = 'Use Test
Go
SELECT
p.[ProblemNumber],
COUNT(i.[IncidentNumber]) as ActiveIncidents
FROM [Problem] p
JOIN [Incident] i
ON P.[RecID] = i.[ProblemRecID]
WHERE p.[Status] <> ''Closed''
AND i.[Status] = ''Active''
GROUP BY p.[ProblemNumber]',
@attach_query_result_as_file = 0;

SZ1
Please help me to enable me to help others!
Go to Top of Page
   

- Advertisement -