Question : problem with db_mail

I have a procedure that attaches the result of a query to an email, however the attachment is not the same of the select statement.  Can someone offer suggestions why this is happening or provide an alternate solution?

The results of the select statement is:

Material Number      Business Group
123456                  IPG  
123456-001  234567      IPG  
123456-001              ESS  

however the attachment with the results is different (see attached file)
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'[email protected]',
@body='Message Body', 
@subject ='Message Subject',
@profile_name ='ROHS_Profile',
@query = 'SELECT     CAST(ISNULL([Material Number], '') AS CHAR(18)) AS [Material Number], CAST(ISNULL([Business Group], '') AS char(6)) AS [Business Group]
FROM ROHS.dbo.ROHS_TRANSLATION_REPORT', 
@attach_query_result_as_file = 1,
@query_attachment_filename ='Results.txt'

Answer : problem with db_mail

Here it goes again, just in case:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
DECLARE @tableHTML  NVARCHAR(MAX);
declare @strQuery nvarchar(max) ;
 
SET @tableHTML =
    N'

ROHS2 Translation Report

' + N'' + N'' + N'' + CAST ( ( SELECT td = wo.[Material Number], '', td = wo.[Business Group], '', td = wo.[ROHS2_STATUS], '', td = wo.[Work e-mail], '', td = wo.[Title] FROM (select b.Value as [Material Number], a.[Business Group], a.[ROHS2_STATUS], a.[Work e-mail], a.[Title] from ROHS.dbo.ROHS_TRANSLATION_REPORT a cross apply ROHS.dbo.ParmsToList(replace(replace(a.[Material Number], char(10), ''), char(13), '_'), '_') b ) as wo FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
Material NumberBusiness GroupROHS2 StatusSubmitter EmailComments
' ; set @strQuery = N'select cast(isnull(b.Value, '''') as char(18)) as [Material Number], cast(isnull(a.[Business Group], '''') as char(6)) as [Business Group], cast(isnull(a.[ROHS2_Status], '''') as char(20)) as [ROHS2_Status] from ROHS.dbo.ROHS_TRANSLATION_REPORT a cross apply ROHS.dbo.ParmsToList(replace(replace(a.[Material Number], char(10), ''''), char(13), ''_''), ''_'') b' EXEC msdb.dbo.sp_send_dbmail @recipients='[email protected]', @profile_name ='ROHS_Profile', @subject = 'ROHS2 Translation Report', @body = @tableHTML, @body_format = 'HTML', @query = @strQuery, @attach_query_result_as_file = 1, @query_attachment_filename ='Results.txt' ;
Random Solutions  
 
programming4us programming4us