Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sending email with date condition

Author  Topic 

dryankelley
Starting Member

3 Posts

Posted - 2012-09-19 : 16:33:54
Hi everyone. I have a query question. I am brand new to SQL. How would you all advise me on this: I have a table called TABLE1. It has multiple fields, but one field is called DATE/TIME which obviously holds the date and time. I want to write a query so that if there are any records who's DATE/TIME is later than 24 hours from the current time, it sends an email alert to person1@yahoo.com, person2@yahoo.com,and person3@yahoo.com. Thanks in advance for any pointers! (Hope this is in the right place)
Ryan

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-19 : 16:43:03
You can use a query like below and run it from SQL Agent (or any other scheduler software).
IF EXISTS (SELECT * FROM TABLE1 WHERE theDateTimeCol > DATEADD(DAY,1,GETDATE()))
BEGIN
EXEC master.dbo.xp_sendmail
@recipients=N'person1@yahoo.com;person2@yahoo.com;person3@yahoo.com',
@message=N'There are dates more than 24 hours away from now' ;
END
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2012-09-19 : 16:48:15
we've switched from xp_sendmail to sp_send_dbmail. sp_send_dbmail doesn't require any MAPI client on the server but they'll both work.

Be One with the Optimizer
TG
Go to Top of Page

dryankelley
Starting Member

3 Posts

Posted - 2012-09-20 : 08:52:37
Thank you both for your replies! They are really helpful. I have come to the right place for learning SQL in a friendly environment
Go to Top of Page
   

- Advertisement -