Monday, June 18, 2012

Error formatting query, probably invalid parameters [SQLSTATE 42000] (Error 22050)

Ok, so today I had the bright idea to get my MS SQL Server 2008 to run a stored procedure every night that emails me the results of a query so it would be waiting in my inbox first thing every Monday morning.  I created the stored procedure, created the job for SQL Server Agent to run, and tried running the job.  The job failed?  What?  I manually ran my stored procedure just to make sure it was working and it emailed me the results of the query just like it was supposed to.  I looked at the history of the job and found this error:

Error formatting query, probably invalid parameters [SQLSTATE 42000] (Error 22050).  The step failed.

Now wait a minute, I know my query is good because I just manually ran the stored procedure and it worked, so what is wrong?  It took a minute then it hit me, the domain account that SQL Server Agent uses didn't have permission to access the database.  So, I added the SQL Server Agent account to the database, gave it execute permissions on the stored procedure, and success!  The job now runs successfully on schedule.

Here is what my stored procedure looks like that returns the query results in the body of the email:


EXEC msdb.dbo.sp_send_dbmail
@body = 'The first line of the message body',
@subject = 'This is the subject line',
@profile_name = 'DBMail',
@recipients = 'someone@somewhere.com',
@execute_query_database = 'DatabaseName',
@attach_query_result_as_file = 0,

@query = 'SELECT * FROM TABLE'


1 comment:

  1. Nice & Informative Blog ! If you are facing QuickBooks Error H202 don’t hesitate to dial our toll free number 1-800-986-4607. We are providing best & instant support.

    ReplyDelete