Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with query syntax of EXEC msdb.dbo.sp_send_dbmail

Posted on 2008-06-25
5
Medium Priority
?
1,886 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 1200 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 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