2016-02-25 42 views
0

我試圖創建一個通知系統,在我們的ERP數據庫中某些東西安裝不正確時發出警報。我有一個報告,我手動每天運行檢查,看看有什麼需要我解決的。我不想這樣做,我希望報告通過電子郵件發送給我或發送給我的某種通知。爲訂閱報告創建存儲過程

有人建議如下:

  • 創建SSRS報告(完成;如果報告沒有任何數據沒有什麼解決是否有數據,我需要解決的東西)

  • 創建一個存儲過程,將引發一個錯誤,如果沒有數據

  • 的SSRS報告將使用存儲過程數據集
  • 訂閱報告,並將其如果數據存在

我卡在第二部分與存儲過程將只發送一封電子郵件,這是我的代碼:

CREATE PROCEDURE JOBNOMATS 
AS 
SELECT CASE 
      WHEN (SELECT "JobHead"."JobNum", "JobHead"."Company", "JobHead"."PartDescription", "JobHead"."ReqDueDate", "JobMtl"."PartNum", "JobMtl"."MtlSeq", "JobHead"."InCopyList", "JobHead"."Plant" 
      FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON ("JobHead"."Company"="JobMtl"."Company") AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
      WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB')) IS NULL 
THEN RAISERROR(50001,16,1) 
ELSE SELECT "JobHead"."JobNum", "JobHead"."Company", "JobHead"."PartDescription", "JobHead"."ReqDueDate", "JobMtl"."PartNum", "JobMtl"."MtlSeq", "JobHead"."InCopyList", "JobHead"."Plant" 
      FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON ("JobHead"."Company"="JobMtl"."Company") AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
      WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB') 

,但我得到這兩個錯誤:

Msg 156,Level 15,State 1,Procedure JOBNOMATS,Line 7在關鍵字'RAISERROR'附近有錯誤的 語法。


消息156,級別15,狀態1,過程JOBNOMATS,關鍵字 'ELSE' 線附近不正確9語法 。

+1

對於初學者,你的'SELECT CASE'需要和'END' –

+0

@RickS謝謝 - 我應該只添加一個END還是每次評估之後? – Machzy

回答

0

你不能在CASE語句中這樣做。 你可以嘗試這樣的事情......

CREATE PROCEDURE JOBNOMATS 
AS 
BEGIN 
    IF NOT EXISTS(
     SELECT NULL 
     FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" 
      LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON 
       ("JobHead"."Company"="JobMtl"."Company") 
      AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
     WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL 
     AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB') 
    ) 
    BEGIN 
     RAISERROR(50001,16,1); 
    END 

    SELECT "JobHead"."JobNum", "JobHead"."Company", "JobHead"."PartDescription", "JobHead"."ReqDueDate", "JobMtl"."PartNum", "JobMtl"."MtlSeq", "JobHead"."InCopyList", "JobHead"."Plant" 
    FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON ("JobHead"."Company"="JobMtl"."Company") AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
    WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB') 

END 

有更好的方法來做到這一點。 兩次運行相同的查詢有點浪費。

您也可以通過檢查@@ ROWCOUNT在執行查詢後,提高如果是0

CREATE PROCEDURE JOBNOMATS 
AS 
BEGIN 
    SELECT "JobHead"."JobNum", "JobHead"."Company", "JobHead"."PartDescription", "JobHead"."ReqDueDate", "JobMtl"."PartNum", "JobMtl"."MtlSeq", "JobHead"."InCopyList", "JobHead"."Plant" 
    FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON ("JobHead"."Company"="JobMtl"."Company") AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
    WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB') 

    IF @@ROWCOUNT = 0 
     RAISERROR(50001,16,1); 

END 

OR

你也可以選擇結果到一個臨時表中的錯誤,並如果在臨時表中的記錄數爲0,引發錯誤......否則選擇一切從臨時表...

CREATE PROCEDURE JOBNOMATS 
AS 
BEGIN 
    SELECT "JobHead"."JobNum", "JobHead"."Company", "JobHead"."PartDescription", "JobHead"."ReqDueDate", "JobMtl"."PartNum", "JobMtl"."MtlSeq", "JobHead"."InCopyList", "JobHead"."Plant" 
    INTO #TEMP 
    FROM {oj "Epicor10Live"."Erp"."JobHead" "JobHead" LEFT OUTER JOIN "Epicor10Live"."Erp"."JobMtl" "JobMtl" ON ("JobHead"."Company"="JobMtl"."Company") AND ("JobHead"."JobNum"="JobMtl"."JobNum")} 
    WHERE "JobHead"."Company"='011' AND "JobMtl"."PartNum" IS NULL AND NOT ("JobHead"."JobNum"='95057' OR "JobHead"."JobNum"='AISMNJOB') 

    IF (SELECT COUNT(1) FROM #TEMP) = 0 
     RAISERROR(50001,16,1); 

    --DO MORE STUFF IF YOU WANT TO... 

    SELECT * 
    FROM #TEMP 
END 

一切都取決於查詢有多貴

+0

非常感謝!最後一個似乎是最高效的。 – Machzy

+0

我會用第二個...檢查@@ ROWCOUNT。但這是你的電話。 – Spock

+0

好的,你顯然比我更瞭解這件事。第二個是! – Machzy