SQL JOB - Send Attachement

Hi All

I'm trying to work out why I cannot run the below script in SQL 2014 via Job, the script is below

EXEC msdb.dbo.sp_send_dbmail
	@profile_name = 'Reports',
	@recipients = 'test@test.com',
	@subject = 'Redeemed Vouchers - Attachment',
	@body = 'Hello, see the attached file Redeemed Vouchers.',
	@query = 'EXEC TunnelCosmos.dbo.Sirius_Report_Groupon', 
	@query_result_header = 1,
	@query_no_truncate = 1,
	@query_result_no_padding = 0,
	@attach_query_result_as_file = 1,
	@query_attachment_filename = 'Redeemed Vouchers.txt'

Open in new window


I can run the above script by executing it in the query window, and I'm able to receive the Redeemed Vouchers.txt, open it and view it's contents. However if I create a SQL Job and let it run I get an error, the history says:

01/29/2018 09:09:53,Report - Redeemed Vouchers,Error,2,IS-A-SQL01,Report - Redeemed Vouchers,Email Report with Attachment,,Executed as user: ISAA-SQL01\sqlserver. Failed to initialize sqlcmd library with error number -2147467259. [SQLSTATE 42000] (Error 22050).  The step failed.,00:00:00,16,22050,,,,0

Open in new window


What do you think may be causing this error?

Thanks All.
Stevie ZakhourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak ChauhanSQL Server DBACommented:
This seems permissions issue. Please check the permissions for user ISAA-SQL01\sqlserver. You are able to execute via query window because your credentials is in use but sql agent execute the job by which sql agent service is running.

Permission needs to be checked. Or you can grant sysadmin permission to sql agent account if there is no security concern.

Permission on directory where text file generates.
permission on database in which you are executing stored proc
permission on sp_senddbmail
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
When you're running the script, you're using your user and it for sure has permission to access the attachment file in the disk.
When you're running as a job, the job owner also needs to have permission to access the attachment file in the disk. Provide the necessary permissions for your SQL Server and SQL Agent accounts to access the file.
0
Stevie ZakhourAuthor Commented:
Thanks all, makes sense!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Stored Proc

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.