?
Solved

sql dbmail attach query parameters

Posted on 2014-07-18
3
Medium Priority
?
364 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 1000 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 1000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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