Solved

problem with query syntax of EXEC msdb.dbo.sp_send_dbmail

Posted on 2008-06-25
5
1,848 Views
Last Modified: 2012-05-05
I have a script that alerts me if new records have been added to a table within the past 24 hours:

if not exists (select top 1 * from dbo.mytable where datediff(ss,UpdateDate,getdate()) < 86400)      
      begin
            EXEC msdb.dbo.sp_send_dbmail
            @recipients = 'myemail@email.comt',            
            @subject = 'Database alert: No new Data within past 24 hours!',            
            @importance = 'high';
      end

While this works great, I'd really like to include the date last updated in the body of the message, something like: SELECT top 1 'last updated: ', UpdateDate from dbo.mytable order by UpdateDate desc

I tried setting this equal to the @query parameter as such:
@query = 'select top 1 'last updated: ',UpdateDate from dbo.mytable order by UpdateDate desc',

...But i see the error: 'Incorrect syntax near 'last'.  I also tried escaping the ' by using two single quotes on either side of last updated (@query = 'select top 1 ''last updated: '',UpdateDate from dbo.mytable order by UpdateDate desc)

..But I see the error: 'Error formatting query, probably invalid parameters'.

Does anyone know how to fix this?

Thanks in advance.



0
Comment
Question by:hennessym
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 300 total points
ID: 21866154
try this:

@query = 'select top 1 ''LastUpdated: '' + CONVERT(VARCHAR(10), UpdateDate, 101) from dbo.mytable order by UpdateDate desc',
0
 
LVL 1

Author Comment

by:hennessym
ID: 21866329
Thanks for the quick response, chap!

I tried your suggestion but am still seeing 'Error formatting query, probably invalid parameters'
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21866373
OK, try it without the text to see if it will send the query out:

@query = 'select top 1 UpdateDate, from dbo.mytable order by UpdateDate desc',
0
 
LVL 1

Author Comment

by:hennessym
ID: 21866670
Thanks Chap - that revealed that SQL Server was looking for dbo.mytable in the msdb database where the msdb.dbo.sp_send_dbmail resides.  Once I specified the database, the stored procedure successfully executed.

Now, however, I'm seeing the number of rows affected despite having SET NOCOUNT ON as the first line of code in my stored procedure, e.g.:

ALTER procedure [dbo].[NewDataCheck] AS

SET NOCOUNT ON

Any ideas?

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21867450
Thats a new question...one where you'll need to post your procedure code.  
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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