为订阅的报告创建存储过程

时间:2016-02-25 17:15:21

标签: sql-server stored-procedures reporting-services

我正在尝试创建一个通知系统,在我们的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'附近的语法。


  

Msg 156,Level 15,State 1,Procedure JOBNOMATS,第9行不正确   关键字'ELSE'附近的语法。

1 个答案:

答案 0 :(得分: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

全部取决于查询的费用