-1

If I have two SQL scripts in SQL 2012, and I want to receive an email if one of the scripts returns information and the other does not, how would i set that up?

select * 
from 
    doc_queue_pend 
where 
    create_timestamp < DATEADD(Minute, -20, GETDATE());
--want to see nothing

select *
from 
    doc_queue_final 
where 
    create_timestamp > DATEADD(Minute, -20, GETDATE());
--want to see something
Termininja
  • 6,620
  • 12
  • 48
  • 49
Lindsay
  • 1
  • 1
  • 4

1 Answers1

0

count the results of each query and then use an if statement to decide whether or not to send an email.

declare @doc_queue_pend  int 
declare @doc_queue_final int

select @doc_queue_pend = count(*) 
from doc_queue_pend 
where create_timestamp < DATEADD(Minute, -20, GETDATE());

select @doc_queue_final = count(*)
from doc_queue_final 
where create_timestamp > DATEADD(Minute, -20, GETDATE());

if @doc_queue_pend = 0 and @doc_queue_final > 0
begin
    exec sp_send_dbmail ...
end
T I
  • 9,785
  • 4
  • 29
  • 51