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
 General SQL Server Forums
 New to SQL Server Programming
 Complex queries within msdb.dbo.sp_send_dbmail

Author  Topic 

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-19 : 14:23:22
Having done a little research it appears that you cannot use variables within SQL's jobs. Is this correct?

I can now send emails through SQL 2005 (thanks visakh16 =) using simple queries. The goal was to use these jobs to send out data from more complex queries.

What are my options to do this?

/* Primary membership classes that are used within the IN statements */
--DROP TABLE #PrimaryClasses
CREATE TABLE #PrimaryClasses (classID int)
INSERT INTO #PrimaryClasses
SELECT 22 UNION ALL
SELECT 25 UNION ALL
SELECT 28 UNION ALL
SELECT 29 UNION ALL
SELECT 30 UNION ALL
SELECT 36 UNION ALL
SELECT 37 UNION ALL
SELECT 38 UNION ALL
SELECT 39 UNION ALL
SELECT 50 UNION ALL
SELECT 51 UNION ALL
SELECT 52 UNION ALL
SELECT 63 UNION ALL
SELECT 64 UNION ALL
SELECT 65

SELECT tblFacility.vcFacilityName AS Club,
COUNT(CASE WHEN
tblmembers.sistatusid = '1'
AND classid IS NOT NULL
THEN tblmembers.vcMemberID ELSE NULL END) AS [Memberships]
FROM tblMembers INNER JOIN
tblContracts ON tblMembers.iMembershipContractid = tblContracts.iContractId INNER JOIN
tblFacility ON tblMembers.vcCurrentFacilityID = tblFacility.vcFacilityID LEFT OUTER JOIN
[#PrimaryClasses] ON classID = tblContracts.siMembershipClassId
where tblFacility.vcFacilityName <> 'Corporate' and tblMembers.cMemberCategory = 'M'
GROUP BY tblFacility.vcFacilityName

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-19 : 14:58:44
Make the query text stored in a variable.
Assign that variable to matching parameter the sp_dbsendmail procedure.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-19 : 15:01:52
Thanks Peso. I'll do some searching to decifer that =) Can you give me an example or a link to a page that describes how to do that in more detail?
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-19 : 15:04:44
* EDIT * GOT IT!!!

Thanks!
Go to Top of Page
   

- Advertisement -