I'm trying to use sp_send_dbmail to send the results of a query through a SQLAgent job in SQL Server 2014. I believe I have my DBMail profile set up properly but when running this:
exec msdb.dbo.sp_send_dbmail
@profile = 'TestProfile',
@recipients = '[email protected]',
@subject = 'Test',
@query = 'SELECT id FROM TestTable',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'TestValues.txt'
I get the following error message:
Failed to initialize sqlcmd library with error number -2147467259.
Googling this error message didn't turn up anything useful, likely due to the generic error number. Anyone have some insight into this error message?



@query, @attach_query_result and @query_attachment_filename, then replace them with just a simple@bodymessage, it works fine. As soon as I put@queryback in, it fails. I could see it being a permission issue through SqlAgent but I'm just trying to run the command from a Query window with my admin user at the moment.@queryvariable or specifying a@execute_query_databaseparameter. Either of the following fixes my issue:@query = 'select id from dbo.testDB.TestTableor@execute_query_database = 'testDB'