Solved

sql dbmail attach query parameters

Posted on 2014-07-18
3
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
Philip Portnoy earned 250 total points
ID: 40205430
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
ID: 40205627
@query = 'finishedgoods.dbo.FG_SP_EMAIL_Rpt_PDICustomerUnitsSoldSN ''7/1/2014'' ''7/17/2014'' 2'
0
 

Author Comment

by:celetex
ID: 40206272
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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