2016-01-12 33 views
-1

如果我在SQL 2012中有兩個SQL腳本,並且如果其中一個腳本返回信息而其他腳本沒有收到電子郵件,我將如何設置該腳本?使用條件創建SQL作業

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 

回答

0

統計每個查詢的結果,然後使用if語句來決定是否要發送電子郵件。

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 
+0

所以我改變了SQL查詢到這個和當我設置一個導出任務,我得到這個消息: – Lindsay