email multiple query results

I am trying to email multiple query results in the body of an email. My expected results are as follows:

The following records generated errors but were not loaded
1  1234 John Doe jdoe@someCompany.com C:\ 033010 48


The following files were loaded but with Errors

1  1236 Jane Doe jadoe@someCompany.com C:\ 033010 48

I was wondering if anyone could help me with the syntax to get this done thanks. I added some SQL that I have been working on.
DECLARE @DayOfWeek VARCHAR(10)
SET  @DayOfWeek = dbo.udf_DayOfWeek(GETDATE())

IF @DayOfWeek = 'Tuesday'
BEGIN

EXEC msdb.dbo.sp_send_dbmail 

@recipients = 'me@somecompany.com',
@subject = 'Employee Audit',
@body = "'The following files errors were generated but not loaded' + @query_1",
		"The following files were loaded but with Errors" + @query_2",
 
@query = 'SELECT     LogID, EmplID, EmpName, EmailAddress, Path, DateSent, ErrorLength
FROM         audit.LoadLog
WHERE     (Error  is not null) ',

--@query = 'SELECT     LogID, PayorID, JobName, EmailAddress, Path, DateSent, ErrorLength
--FROM         audit.LoadLog
--WHERE     (ErrorLength is null) 

@attach_query_result_as_file = 0;
END
ELSE
EXIT

Open in new window

earngreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this:
DECLARE @DayOfWeek VARCHAR(10)
SET  @DayOfWeek = dbo.udf_DayOfWeek(GETDATE())

IF @DayOfWeek = 'Tuesday'
BEGIN

EXEC msdb.dbo.sp_send_dbmail 

@recipients = 'me@somecompany.com',
@subject = 'Employee Audit',
@body = '',
@query = '
SELECT  'The following files errors were generated but not loaded', null, null, null, null, null, null, null
UNION ALL
SELECT     null,LogID, EmplID, EmpName, EmailAddress, Path, DateSent, ErrorLength
FROM         audit.LoadLog
WHERE     (Error  is not null)  
SELECT  'The following files were loaded but with Errors', null, null, null, null, null, null, null
UNION ALL
SELECT     null, LogID, PayorID, JobName, EmailAddress, Path, DateSent, ErrorLength
FROM         audit.LoadLog
WHERE     (ErrorLength is null) 
',
@attach_query_result_as_file = 0;
END
ELSE
EXIT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LowfatspreadCommented:
you need to set up the @body text  before calling the procedure it doesn't work passing the data as a concatenation  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.