I need to create a stored procedure that upon exceution checks if any new rows have been added to a table within the past 12 hours. If not, an warning email must be sent to a recipient.
I have the procedures for sending the email, but the problem is the query itself. I imagine I’d have to make an sql command that uses current date and compares that to the dates in the rows. But I’m a complete beginner in SQL so I can’t even use the right words to find anything on google.
Short version:
Using MS SQL Server 2005, how can I check against the dates, then return a result based on whether new rows were created within the last 12 hours, and use that result to decide whether or not to send email?
Say your date field in the table is ‘CreateDate’ and it’s of type DateTime. Your time to compare with is: GETDATE() (which returns date + time) To get the datetime value of 12 hours before that, is done using DATEADD: DATEADD(hour, -12, GETDATE())
so if we want the # of rows added in the last 12 hours, we’ll do:
in your proc, you’ve to store the result of this query into a variable and check if it’s > 0, so:
and then you’ll check the @amount variable if it’s > 0.