Question : How to build and send multiple tables using sp_send_dbmail

I am attempting to send an email with an html table embedded in it from sql db mail. I am unable to build separate html tables and send them in one email. Any ideas on how to do this?

The error message i am getting is:
Msg 102, Level 15, State 1, Line 66
Incorrect syntax near '+'.

I have tried variations of + including commas, AND, JOIN [anything i can think of]. I have also tried using multiple @body tags. all without success.

My sql is listed below.

Thank you,
Jason
Code Snippet:
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:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
DECLARE @tableHTML NVARCHAR(MAX) ;

SET @tableHTML = 
	N'

Modified Employees

' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + CAST (( SELECT td = [AssociateId], '', td = [Name], '', td = RIGHT([SSN],4), '', td = [JobCode], '', td = LEFT([EffDate],12), '', td = [DeptCode], '', td = [Title], '', td = [Description], '', td = [Manager] FROM [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView] WHERE [Modified] = 1 FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
Employee NumberFull NamePassword IDJob CodeEffective DateDepartment CodeDepartmentDescriptionManager
' DECLARE @tableHTML2 NVARCHAR(MAX) ; SET @tableHTML2 = N'

New Employees

' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + N'' + CAST (( SELECT td = [AssociateId], '', td = [Name], '', td = RIGHT([SSN],4), '', td = [JobCode], '', td = LEFT([EffDate],12), '', td = [DeptCode], '', td = [Title], '', td = [Description], '', td = [Manager] FROM [AssociateIndexTest].[dbo].[FSPI_AssociateIndexView] WHERE [Modified] = 2 FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
Employee NumberFull NamePassword IDJob CodeStart DateDepartment CodeDepartmentDescriptionManager
' ; EXEC msdb.dbo.sp_send_dbmail @profile_name = 'WS0071_SQL_EMAIL', @recipients = '[email protected]', @body = @tableHTML + @tableHTML2, @body_format = 'HTML', @subject = 'These associates have been modified';

Answer : How to build and send multiple tables using sp_send_dbmail

change the last part as this
1:
2:
3:
4:
5:
6:
7:
SET @tableHTML = @tableHTML + @tableHTML2
EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'WS0071_SQL_EMAIL',
     @recipients = '[email protected]',
         @body =  @tableHTML,
         @body_format = 'HTML',
     @subject = 'These associates have been modified';
Random Solutions  
 
programming4us programming4us