DB mail Query parameter not working

On one of my servers the query parameter just wasn’t working.  I kept getting this error:

Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
Query execution failed: Msg 208, Level 16, State 1, Server UFOsqlAB, Line 1
Invalid object name ‘CountySalesTax’.

What to do?  The solution?  Simple actually.  Include the statement “use database;” before the query as in:

Exec msdb.dbo.sp_send_dbmail
@Query = ‘Use MyDb; select * from mytable’,
@body = ‘Job complete’;

Leave a Reply

Your email address will not be published. Required fields are marked *