I was running into an issue when creating and implementing a new SQL server agent job to run and email the results of a simple query:
EXEC ms.dbo.sp_send_dbmail
@profile_name = 'Main DB Mail Profile',
@recipients = '[email protected]',
@subject = 'Basket Report',
@query = N'Select Store, Date, Sum(Amount) as DailyTotal, COUNT(CAST(Trans as varchar(30))+CAST(Register as Varchar(30))) as DistinctTransactions
From BasketAnalysis
Where Date = dateadd(day,datediff(day,1,GETDATE()),0)
GROUP BY Store, Date
ORDER BY Store ASC;',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'BasketReport.txt'
I kept running into a mysterious error message in the history log for the task.
Executed as user: NT AUTHORITY\NETWORK SERVICE. Error formatting query, probably invalid parameters [SQLSTATE 42000] (Error 22050). The step failed.
Has anyone had any luck getting more information on these errors and how to resolve them?
Digging around, I found a large number of different potential solutions, that I thought I would try to compile some of them here.