Solved

sql dbmail attach query parameters

Posted on 2014-07-18
3
340 Views
Last Modified: 2014-07-19
I am using SQL 2005 dbmail and am trying to add parameters to the @query parameter:

                  EXEC msdb.dbo.sp_send_dbmail
                        @profile_name = @profile,
                        @recipients = @recipients,
                        @subject = @subject,
                        @body = @body,
                        @body_format = 'HTML',
                        @execute_query_database = @db_name,
                        @importance = 'Normal',
                        @query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN',---NEED TO ADD PARAMETERS TO THIS
                        @attach_query_result_as_file = 1,
                        @query_attachment_filename = @namedfile,
                        @query_result_separator = '      ',
                        @query_result_no_padding=1,
                        @query_result_width = 600

This works fine with the line @query ='finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN' without parameters and with an integer parameter of 2 on the end:

@query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN 2'

What I am trying to do is add additional non integer parameters to this with dates:

@query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN 7/1/2014 7/17/2014 2'

so that there are 3 parameters passed to the stored procedure:  date1 =  7/1/2014 date2 = 7/17/2014 and integer 2.

Any suggestions?  It errors out as soon as I put any of the dates in ''.
0
Comment
Question by:celetex
3 Comments
 
LVL 4

Accepted Solution

by:
Philip Portnoy earned 250 total points
Comment Utility
Have you tried adding "EXEC" in the beginning of the query? Does this query work outside of dbmail scope?
Does it error out when you pass params with their names, like sp_storedprocedure @date1 = 7/1/2014, @date2 = 7/17/2014, @integer=2"?
0
 
LVL 13

Assisted Solution

by:Russell Fox
Russell Fox earned 250 total points
Comment Utility
@query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN ''7/1/2014'' ''7/17/2014'' 2'
0
 

Author Comment

by:celetex
Comment Utility
The SP does work outside of the scope of dbmail.  It returns the same whether or not 'EXEC' is added at the beginning.  I did get it to work combining both answers:

@query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN @start_date=''7/1/2014'', @end_date=''7/17/2014'',  @areaID=''2'''

Evidently it needs the sp parameter name, the double quotes, AND a comma between the parameters.

Thank you both very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now