SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
sunpyo.hong
sunpyo.hong
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 8
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.
GSquared
GSquared
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105399 Visits: 9730
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
dpaproskijr
dpaproskijr
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 231
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.
vconfused
vconfused
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 12
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.
LAW1143
LAW1143
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1042 Visits: 426
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search