troubleshooting Question

sp_send_dbmail @Query with Select statement Questons

Avatar of dm3295
dm3295 asked on
Microsoft SQL Server
2 Comments1 Solution1261 ViewsLast Modified:
General info:  I have a table that has a column type of DateTime (fdUpdateDate) and I am trying to send and error e-mail and showing the rows in this table that match a parameter of @DateTime that was set to the current date earlies in the SQL code.  However, I am having problems with the @query statement recognizing the @DateTime as a parameter and not a sring or at least I think that is what is wrong. I have tried the below with the following errors:
exec msdb..sp_send_dbmail  
@profile_name = 'Mail Test',
@recipients = 'dm3295@att.com',
@body = 'The following error(s) need to be investigated: ',
@copy_recipients = '7192428383@cingularme.com',
@query = 'select * from dbProcess..tbNumOfFilesinDirectories where fdUpdateDate =' + @DateTime ,
@Subject = 'Action:  dbProcess..tbNumOfFIlesinDirectories Error(s) Need Investigation.'

Error:  Doesnt compile
Msg 102, Level 15, State 1, Procedure sp_NumOfFiles_Directories, Line 182
Incorrect syntax near '+'.

Then I tried this as well with the + @DateTime inside the quotes.
exec msdb..sp_send_dbmail
@profile_name = 'Mail Test',
@recipients = 'dm3295@att.com',
@body = 'The following error(s) need to be investigated: ',
@copy_recipients = '7192428324@cingularme.com',
@query = 'select * from dbProcess..tbNumOfFilesinDirectories where fdUpdateDate = + @DateTime',  @Subject = 'Action:  dbProcess..tbNumOfFIlesinDirectories Error(s) Need Investigation.'

Errors:  
(6 row(s) affected)
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 476
Query execution failed: Msg 137, Level 15, State 2, Server WICDTL01DM3295, Line 1
Must declare the scalar variable "@DateTime".
(1 row(s) affected)
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.

NOTE:  I have changed the number in the pager for text to a dummy number because this will be on the internet.
How can I get this to only select the rows that match my date parameter?

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros