Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL Server 2008 SP_Send_dbmail

SQL Server 2008 SP_Send_dbmail

SQL Server 2008 SP_Send_dbmail

I am beating my head against a wall, I am trying to set up a job to run and produce an xls to send to certain members.
If I take the "--" away from "Use DBName" and "Execute as user" I get a MSG 229,Lvl 14,State5,Procedure sp_send_DBmail Line 1 - Execute permission was denied on object
If I put in the "--" for "Use DBName" and keep "Execute as user" I get a MSG 15517,Lvl 16,State 1, Line 2 - Cannot execute as the db principal because the "" doesn't exsist.
I've tried changing/adding/permissions in all dbs, unchecking collation in OE, Checking the SIDs match and I feel I am at a loss. Someone help
Below is my query:
--Execute as user = 'DOMAIN\svc_ACCTNAME'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DBNAME@ABC.com',
@recipients = 'Name@ABC.com',
@subject = 'Report',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'REPORTNAME.xls',
--DECLARE @msg VARCHAR(250);
--declare @query varchar(2048);
--SELECT @msg = 'Please refer to the attached spread sheet for the report.';
@query='Use DBNAME
SELECT "JobOperations"."jmoProcessID" as "Resource ID",
"JobOperations"."jmoProcessShortDescription" as "Resource Description",
"Employees"."lmeEmployeeName" as "Employee Name",
"Jobs"."jmpPartID" as "Part ID",
"Jobs"."jmpJobID" as "Job ID",
"JobOperations"."jmoJobOperationID" as "Seq",
"Jobs"."jmpOrderQuantity" as "WO QTY",
"JobOperations"."jmoQuantityComplete" as "LBR QTY",
("JobOperations"."jmoOperationQuantity" - "JobOperations"."jmoQuantityComplete") as "QTY Delt",
"JobOperations"."jmoEstimatedProductionHours" as "Standard Run Hours",
"JobOperations"."jmoActualProductionHours" as "Actual Hours"
FROM (("DBNAME"."dbo"."Jobs" "Jobs"
LEFT OUTER JOIN "DBAME"."dbo"."JobOperations" "JobOperations" ON "Jobs"."jmpJobID"="JobOperations"."jmoJobID")
LEFT OUTER JOIN "DBNAME"."dbo"."TimecardLines" "TimecardLines" ON (("JobOperations"."jmoJobID"="TimecardLines"."lmlJobID") AND ("JobOperations"."jmoProcessID"="TimecardLines"."lmlProcessID")) AND ("JobOperations"."jmoWorkCenterID"="TimecardLines"."lmlWorkCenterID"))
LEFT OUTER JOIN "DNAME"."dbo"."Employees" "Employees" ON "TimecardLines"."lmlEmployeeID"="Employees"."lmeEmployeeID"
WHERE "timecardlines"."lmlCreatedDate" <= DATEADD(day, DATEDIFF(dd, 0, getdate()-7), 0) AND
"timecardlines"."lmlCreatedDate" > DATEADD(dd, DATEDIFF(dd, 0, getdate()-14), 0)'

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close