Solved

sql dbmail attach query parameters

Posted on 2014-07-18
3
357 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

695 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