Solved

sql dbmail attach query parameters

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sqlquerystress - To test db performance 8 42
Linked Server Issue with SQL2012 3 26
Deal with apostrophe in stored procedures 8 42
abs operation in sql server 15 14
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

770 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