Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Cannot run query as job, but can run it as a query Expand / Collapse
Author
Message
Posted Thursday, January 6, 2011 10:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 20, 2011 1:30 PM
Points: 2, 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.
Post #1043889
Posted Thursday, January 6, 2011 11:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
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
Post #1043913
Posted Thursday, August 25, 2011 7:39 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 8, 2014 11:15 AM
Points: 12, Visits: 215
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.

Post #1165349
Posted Thursday, February 9, 2012 7:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 16, 2012 2:56 PM
Points: 1, 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.
Post #1249720
Posted Thursday, December 27, 2012 1:48 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 9:32 AM
Points: 82, Visits: 296
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.
Post #1400742
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse