Cannot run query as job, but can run it as a query

  • I'm having issues running a job in SMSS 2005. The queries themselves work fine on their own through SQL queries, but when inputted as a job it refuses to run. This is the general query

    EXEC msdb.dbo.sp_send_dbmail @profile_name='ABC MAIL',

    @recipients='xxx@abc.com',

    @subject='Stats report for yesterday',

    @body='See attachment',

    @query='(SELECT * FROM XXX.dbo.fnReportAgentStatsYesterday()) UNION (SELECT * FROM XXX.dbo.fnReportAgentStatsYesterdaySummary()) ORDER BY abcName',

    @attach_query_result_as_file=1,

    @query_attachment_filename='AgentStatsYesterday.csv',

    @query_result_separator='',

    @query_result_no_padding=1

    Here's a log of what happened:

    Job 'Agent Stats Report' : Step 1, 'Report' : Began Executing 2011-01-06 12:02:42

    Msg 22050, Sev 16, State 1: Error formatting query, probably invalid parameters [SQLSTATE 42000]

    Also the function: fnReportAgentStatsYesterdaySummary is linked to another server database and they do not share the same domain.

    I'm guessing it's something to do with SQL agent, according to other posts, but if anyone knows anything, that would be much appreciated.

  • Try assigning the values to variables, then using the variables when you call the proc.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Add @append_query_error = 1 to the call to sp_send_dbmail

    That way an email will be sent with the results of the attempt to run the query.

  • Did you find a fix for this? We are having the same issue. Sometimes the job will work and other times (as in other days, same time) it won't. Actually we have been having this on a number of our jobs. We were able to get the same error when running manually as well (i.e. job fails, adjusted job time and still failed, manually run code in job and it fails, manually run code in job and it works) -- no rhyme or reason can be found yet. Instead it returns:

    Msg 22050, Sev 16, State 1: Error formatting query, probably invalid parameters [SQLSTATE 42000]

    We are passing variables to the same sproc.

  • silly possibility, make sure the user SQL Agent runs under has access to the database it queries.

    that was my problem, found it using the @append output option.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply