Solved

problem with query syntax of EXEC msdb.dbo.sp_send_dbmail

Posted on 2008-06-25
5
1,844 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now