Solved

problem with query syntax of EXEC msdb.dbo.sp_send_dbmail

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore 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.
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…

740 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