存储过程 - SQL Server 2014中的BEGIN END ELSE不插入记录

时间:2017-10-09 18:20:54

标签: sql-server stored-procedures

*更新*

感谢@wei_dba帮助我指明正确的方向,我能够看到我需要在else代码块中重新声明我的开始和结束日期变量。

我有一个正在运行的存储过程。当在部分中运行时,该过程按预期运行,但是当使用exec sp_name整体运行时,该过程退出,好像没有任何要插入的东西,我很困惑为什么会发生这种情况。

我确实有中间步骤,将数据插入@TempTable#TempTable

对于故障排除我有以下,运行正常:

IF NOT EXISTS (SELECT TOP 1 * FROM sysobjects 
               WHERE name = 'c_Lab_Rad_Order_Utilization_test' AND xtype = 'U')
BEGIN
    select 'Table DNE'
END 
ELSE
    select 'Table Exists'

以上正确评估为Table Exists,因为事实确实如此。由于某种原因,ELSE语句没有像我预期的那样运行,这意味着它根本没有运行,我希望它今天运行。

以下是查询的ELSE部分:

IF NOT EXISTS (SELECT TOP 1 * FROM sysobjects 
               WHERE name = 'c_Lab_Rad_Order_Utilization_test' AND xtype = 'U')
BEGIN
    ...
END
ELSE BEGIN
    -- If the IT NOT EXISTS...Evaluates to FALSE, meaning data is returned,   then...
    DECLARE @T2 TABLE (
       PK INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
       , MRN                 INT
       , Encounter           INT
       , Order_No            INT
       , Order_Loc           VARCHAR(100)
       , Svc_Cd              VARCHAR(100)
       , Svc_Desc            VARCHAR(500)
       , Ord_Set_ID          VARCHAR(200)
       , Ordering_Party      VARCHAR(500)
       , Ord_Pty_Number      CHAR(6)
       , Ord_Pty_Spclty      CHAR(5)
       , Performing_Dept     VARCHAR(100)
       , Svc_Dept            VARCHAR(100)
       , Svc_Sub_Dept        VARCHAR(100)
       , Ord_Occ_No          INT
       , Ord_Occ_Obj_ID      INT
       , Ord_Entry_DTime     DATETIME
       , Ord_Start_DTime     DATETIME
       , Ord_Stop_DTime      DATETIME
       , Order_Status        VARCHAR(250)
       , Order_Occ_sts_cd    CHAR(1)
       , Order_Occ_Status    VARCHAR(250)
       , Admit_DateTime      DATETIME
       , Admit_Year          VARCHAR(100) 
       , Dup_Order           CHAR(1)
    );

    WITH T2 AS (
       SELECT A.med_rec_no
       , A.episode_no
       , A.ord_no
       , A.ord_loc
       , A.svc_cd
       , A.svc_desc
       , A.ord_set_id
       , A.pty_name
       , A.pty_cd
       , E.src_spclty_cd
       , A.perf_dept
       , A.svc_dept
       , A.svc_subdept
       , A.ord_occr_no
       , A.ord_occr_obj_id
       , A.ent_dtime
       , A.Order_Str_Dtime
       , A.stp_dtime
       , C.ord_sts_modf  AS [Order_Status]
       , B.occr_sts_cd
       , B.occr_sts_modf AS [Order_Occ_Status]
       , D.adm_dtime
       , YEAR(d.adm_Dtime) AS [Admit_Year]
       , a.ovrd_dup_ind

       FROM smsdss.c_sr_orders_finance_rpt_v    AS A
       INNER JOIN SMSMIR.ord_occr_sts_modf_mstr AS B
       ON A.Occr_Sts = B.occr_sts_modf_cd
       INNER JOIN SMSMIR.ord_sts_modf_mstr      AS C
       ON A.ord_sts = C.ord_sts_modf_cd
       LEFT OUTER JOIN smsmir.acct              AS D
       ON A.episode_no = SUBSTRING(D.pt_id, 5, 8)
       LEFT OUTER JOIN smsdss.pract_dim_v       AS E
       ON A.pty_cd = E.src_pract_no
       AND E.orgz_cd = 'S0X0'

       WHERE LEFT(A.svc_cd, 3) IN (
              '004'   -- Lab
              , '005' -- Rad
              , '006' -- EKG
              , '013' -- Cat Scan
              , '014' -- Ultrasound
              , '023' -- MRI
       )
       AND C.ord_sts_modf IN ('Complete', 'Discontinue')
       AND D.adm_date >= @start -- replace with @start for prod
       AND D.adm_date <  @end -- replace with @end for prod
       AND LEFT(A.episode_no, 1) IN ('1', '8')
       AND LEFT(A.episode_no, 4) != '1999'
       -- CAN ADD UNITIZED ACCOUNTS BACK IN IF NEEDED
       --AND LEFT(A.episode_no, 1) != '7'
    )

    INSERT INTO @T2
    SELECT * FROM T2;

 select * from @t2; --nothing results

    SELECT T2.MRN
    , T2.Encounter
    , T2.Order_No
    , T2.Order_Loc
    , CASE
           WHEN T2.Order_Loc = 'EDICMS'
                  THEN 'ED'
           WHEN T2.Order_Loc != 'EDICMS'
                  AND LEFT(T2.Encounter, 1) = '8'
                  THEN 'ED'
           WHEN T2.Order_Loc != 'EDICMS'
                  AND T2.Ord_Pty_Spclty = 'EMRED'
                  THEN 'ED'
           ELSE 'IP'
      END AS [ED_IP_FLAG]
    , T2.svc_cd
    , T2.Svc_Desc
    , T2.Ord_Set_ID
    , T2.Ord_Pty_Number
    , T2.Ordering_Party
    , T2.Ord_Pty_Spclty
    , T2.Performing_Dept
    , CASE
           WHEN T2.Performing_Dept='BMHEKG' THEN 'EKG'
           WHEN T2.Svc_Sub_Dept IN (
           '114', '7', '2', '137', '127', '3', '135', '6'  --117
           )
                  THEN 'Laboratory'
           WHEN T2.Svc_Sub_Dept IN (
           '1045', '16', '13', '12', '11', '14', '17', '10', '1004' --133
           )
                  THEN 'Radiology'
      END AS [Svc_Dept_Desc]
    , T2.Svc_sub_Dept
    , CASE
           WHEN T2.Performing_Dept = 'BMHEKG' THEN 'EKG'
           WHEN T2.Svc_Sub_Dept = '114'  THEN 'Cytology'
           WHEN T2.Svc_Sub_Dept = '7'    THEN 'Hematology'
           WHEN T2.Svc_Sub_Dept = '2'    THEN 'Blood Bank'
           WHEN T2.Svc_Sub_Dept = '137'  THEN 'Serology'
           WHEN T2.Svc_Sub_Dept = '127'  THEN 'Other'
           WHEN T2.Svc_Sub_Dept = '3'    THEN 'Microbiology'
           WHEN T2.Svc_Sub_Dept = '135'  THEN 'Reference'
           --WHEN t1.Svc_Sub_Dept = '117'  THEN 'Lab Order Only' Remove Per Jim Carr.  SCM 3-25-16
           WHEN T2.Svc_Sub_Dept = '6'    THEN 'Chemistry'
           WHEN T2.Svc_Sub_Dept = '1045' THEN 'Mobile PET Scan'
           WHEN T2.Svc_Sub_Dept = '16'   THEN 'Special Procedures'
           WHEN T2.Svc_Sub_Dept = '13'   THEN 'MRI'
           WHEN T2.Svc_Sub_Dept = '12'   THEN 'Mammography'
           WHEN T2.Svc_Sub_Dept = '11'   THEN 'DX Radiology'
           --WHEN t1.Svc_Sub_Dept = '133'  THEN 'Rad Order Only' Remove Per Chris Schneider. SCM 3-25-16
           WHEN T2.Svc_Sub_Dept = '14'   THEN 'Nuclear Medicine'
           WHEN T2.Svc_Sub_Dept = '17'   THEN 'Ultrasound'
           WHEN T2.Svc_Sub_Dept = '10'   THEN 'Cat Scan'
           WHEN T2.Svc_Sub_Dept = '1004' THEN 'BNL'
      END AS [Svc_Sub_Dept_Desc]
    , T2.Ord_Occ_No
    , T2.Ord_Occ_Obj_ID
    , T2.Ord_Entry_DTime
    , T2.Ord_Start_DTime
    , T2.Ord_Stop_DTime
    , T2.Order_Status
    , T2.Order_Occ_Status
    , T2.Admit_DateTime
    , T2.Dup_Order
    , T2.Admit_Year
    , RunDate = CAST(GETDATE() AS date)
    , RunDateTime = GETDATE()

    INTO #TEMPA

    FROM @T2 T2
    WHERE T2.Order_Occ_sts_cd = '4'
    AND T2.SVC_SUB_DEPT NOT IN ('133','117')

 select * from #tempa --nothing results
    ;

    SELECT TEMPA.*
    , RN = ROW_NUMBER() OVER(
        PARTITION BY Encounter
                    , Order_No
                    , Ord_Occ_No
                    , Ord_Occ_Obj_ID
        ORDER BY Encounter
                    , Order_No
                    , Ord_Occ_No
                    , Ord_Occ_Obj_ID
    )
    INTO #TEMPB
    FROM #TEMPA AS TEMPA
    WHERE TEMPA.ED_IP_FLAG IN ('IP', 'ED')
    AND TEMPA.Svc_Dept_Desc IN ('Laboratory', 'Radiology')
    ;

    INSERT INTO smsdss.c_Lab_Rad_Order_Utilization_test

    SELECT TEMPB.*
    , DATEPART(YEAR, TEMPB.Ord_Entry_DTime)    AS Ord_Ent_Yr
    , DATEPART(MONTH, TEMPB.Ord_Entry_DTime)   AS Ord_Ent_Mo
    , DATEPART(HOUR, TEMPB.Ord_Entry_DTime)    AS Ord_Ent_Hr
    , DATEPART(QUARTER, TEMPB.Ord_Entry_DTime) AS Ord_Qtr
    , DATEPART(YEAR, TEMPB.Ord_Start_DTime)    AS Ord_Start_Yr
    , DATEPART(MONTH, TEMPB.Ord_Start_DTime)   AS Ord_Start_Mo
    , DATEPART(HOUR, TEMPB.Ord_Start_DTime)    AS Ord_Start_Hr
    , DATEPART(QUARTER, TEMPB.Ord_Start_DTime) AS Ord_Start_Qtr
    , DATEPART(YEAR, TEMPB.Ord_Stop_DTime)     AS Ord_Stop_Yr
    , DATEPART(MONTH, TEMPB.Ord_Stop_DTime)    AS Ord_Stop_Mo
    , DATEPART(HOUR, TEMPB.Ord_Stop_DTime)     AS Ord_Stop_Hr
    , DATEPART(QUARTER, TEMPB.Ord_Stop_DTime)  AS Ord_Stop_Qtr
    , DATEPART(MONTH, TEMPB.Admit_DateTime)    AS Adm_Mo
    , DATEPART(HOUR, TEMPB.Admit_DateTime)     AS Adm_Hr
    , DATEPART(QUARTER, TEMPB.Admit_DateTime)  AS Adm_Qtr

    FROM #TEMPB AS TEMPB

    WHERE CAST(GETDATE() AS DATE) <> ISNULL((SELECT MAX(RUNDATE) FROM smsdss.c_Lab_Rad_Order_Utilization_test), GETDATE() - 1)
    AND TEMPB.Ord_Occ_No NOT IN (
        SELECT DISTINCT(Ord_Occ_No)
        FROM smsdss.c_Lab_Rad_Order_Utilization_test
    ) --basically superfluous AND statement
    AND TEMPB.RN = 1    
    ;

select * from #tempb --nothing results

    DROP TABLE #TEMPA;
    DROP TABLE #TEMPB;
END
;

上面的else部分没有运行(即使标记为可能多余的AND子句被注释掉),但是当我在另一个查询中使用相同的WHERE子句时:

else
    --select 'table exist'
    -- DATE VARIABLE DECLARATION AND SET
    DECLARE @END DATETIME;
    DECLARE @START DATETIME;
    -- Beginning of current month
    SET @END = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0);
    -- Beginning of previous month
    SET @START = DATEADD(MM, DATEDIFF(mm, 0, GETDATE()) -1, 0);

    declare @t1 table (
        ptno_num varchar(12)
    );

    with t1 as (
        SELECT PtNo_Num
        FROM smsdss.BMH_PLM_PtAcct_V
        WHERE Dsch_Date >= @START
        AND Dsch_Date < @END
        AND Plm_Pt_Acct_Type = 'I'
        AND LEFT(PTNO_NUM, 1) = '1'
        AND LEFT(PTNO_NUM, 4) != '1999'
        AND tot_chg_amt > 0
        AND CAST(GETDATE() AS DATE) <> ISNULL((SELECT MAX(RUNDATE) FROM smsdss.c_Lab_Rad_Order_Utilization_test), GETDATE() - 1)
    )

    insert into @t1
    select * from t1;

    select * from @t1;

我得到了我期望的结果,是不同的数据,但我假设where子句是问题所在。有没有办法逐步完成查询,就像你可以在VBA中单步执行代码一样?

**更新**

我还更新了else语句,将结果放入#TEMPC而不是smsdss.c_lab_rad_order_utilization_test,并且该过程适用于所有where子句。

0 个答案:

没有答案