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 2005 Forums
 Transact-SQL (2005)
 sp_send_dbmail - in 'quotes' hell!

Author  Topic 

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2009-10-07 : 21:28:41
Hi everyone, I'm trying to email query results using database mail, but the @query parameter is making me crazy
This query runs fine:
-----------------------------------
SELECT Address1+ ISNULL(address2, '') + ', ' + city + ', ' + State + ' '+ Zipcode AS 'GH Address', '12' AS RCL, FacilityID, bedcount AS 'Lic. Cap.',
DecBedCount as 'Dec. Lic. Cap.',dbo.fnCountLADCFS(FacilityID)AS #DCFS,
dbo.fnCountLAPROB(FacilityID)AS #PROB, dbo.fnCountLADMH(FacilityID)AS DMH,dbo.fnCountNONLA(FacilityID)AS #NONLA,
(dbo.fnCountLADCFS(FacilityID) + dbo.fnCountLAPROB(FacilityID) +dbo.fnCountLADMH(FacilityID) + dbo.fnCountNONLA(FacilityID))AS'Month End Total', '12-17' as 'Age Range', 'F' as Gender
From dbo.facility


However, after double quoting everything that needs it, the procedure following errors out with:
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 476
Query execution failed: Msg 208, Level 16, State 1, Server SERVER1\SBSSQL, Line 1
Invalid object name 'dbo.Facility'.
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.

--------------------------
EXEC msdb.dbo.sp_send_dbmail
@recipients= 'xxxxxxx@xxxx.xxx',
@Query = 'SELECT Address1 + ISNULL(address2, '''') + '', '' + city + '', '' + State + '' ''+ Zipcode AS ''GH Address'',
''12'' AS RCL, FacilityID, bedcount AS ''LicCap'',
DecBedCount as DecrLicCap, dbo.fnCountLADCFS(FacilityID)AS #DCFS,
dbo.fnCountLAPROB(FacilityID)AS #PROB, dbo.fnCountLADMH(FacilityID)AS DMH,dbo.fnCountNONLA(FacilityID)AS #NONLA,
(dbo.fnCountLADCFS(FacilityID) + dbo.fnCountLAPROB(FacilityID) + dbo.fnCountLADMH(FacilityID) + dbo.fnCountNONLA(FacilityID))AS ''Month End Total'',
''12-17'' AS ''Age Range'', ''F'' AS Gender FROM dbo.Facility',
@subject = 'GH Util',
@attach_query_result_as_file = 0 ;


----------------
-Stephen

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-08 : 00:43:37
It is much easier to wrap your query into a stored procedure and then just call the stored procedure from Database Mail. By using that method, you will not need to worry about single quotes.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-08 : 02:39:59
Also refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2009-10-08 : 10:22:02
Thanks Tara, that worked much better. I just made the query a view and output it to excel using http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926. Thanks once again to everyone else, too!

----------------
-Stephen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-08 : 12:32:55
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -