2012-09-27 156 views
0

在這裏,我試圖獲得有多少條記錄滿足條件的計數,以便我可以在應用程序的圖塊中顯示總數。但是,我沒有成功,你能告訴我如何計數以及在哪裏投入。需要在查詢中計數

(
    @P_FACILITY_KEY_CSV VARCHAR(MAX) 
, @DATEFROM DATETIME 
, @DATETHRU DATETIME 
, @P_TOTALS NUMERIC(1, 0) 
) 
AS 
BEGIN 
      DECLARE @DFACKEY CHAR(4) 
      DECLARE @IRENEWALLCYCLEPERIOD NUMERIC 
      SET @IRENEWALLCYCLEPERIOD = OGEN.READ_PARAM(349, @P_FACILITY_KEY_CSV, '') 
      EXEC OGEN.GET_CODE_FACILITY @P_FACILITY_KEY_CSV, 'OGEN.GEN_M_DOCTOR_MAST', 'BASE', @DFACKEY OUTPUT; 

    IF @P_TOTALS = 1 BEGIN 

     SELECT P.NURSING_UNIT, 
       ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD) DUE_DATE, 
       CONVERT(NUMERIC, ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD) - GETDATE()) REMAINING 
       FROM OGEN.GEN_M_PATIENT_MAST P 
       LEFT OUTER JOIN OGEN.GEN_M_DOCTOR_MAST D ON P.PRIMARY_DOCTOR_KEY = D.DOCTOR_KEY AND D.FACILITY_KEY = @DFACKEY 
       LEFT OUTER JOIN OPTC.ORD_D_SCHEDULED_RECERTS SR ON P.PAT_NUMBER = - SR.PAT_NUMBER 
       LEFT OUTER JOIN OGEN.GEN_M_USER U ON U.USER_ID = SR.RECERT_BY 
       WHERE P.FACILITY_KEY IN(SELECT VALUE FROM OGEN.COMMA_TO_TABLE(@P_FACILITY_KEY_CSV)) 
       AND DISCHARGE_DATE IS NULL 
       AND (((@DATEFROM > 0) AND (OGEN.DATEONLY(ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD)) >= @DATEFROM)) 
        OR 
        (@DATEFROM = 0)) 
       AND (((@DATETHRU > 0) AND (OGEN.DATEONLY(ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD)) <= @DATETHRU)) 
        OR 
        (@DATETHRU = 0)) 

       ORDER BY REMAINING; 

    END ELSE BEGIN 

      SELECT P.*, D.DR_FULL_NAME PRIMARY_DOCTOR_NAME, 
       SR.RECERT_DATE, SR.RECERT_BY, U.USER_NAME, 
       ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD) DUE_DATE, 
       CONVERT(NUMERIC, ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD) - GETDATE()) REMAINING       
      FROM OGEN.GEN_M_PATIENT_MAST P 
      LEFT OUTER JOIN OGEN.GEN_M_DOCTOR_MAST D ON P.PRIMARY_DOCTOR_KEY = D.DOCTOR_KEY AND D.FACILITY_KEY = @DFACKEY 
      LEFT OUTER JOIN OPTC.ORD_D_SCHEDULED_RECERTS SR ON P.PAT_NUMBER = - SR.PAT_NUMBER 
      LEFT OUTER JOIN OGEN.GEN_M_USER U ON U.USER_ID = SR.RECERT_BY 
      WHERE P.FACILITY_KEY IN(SELECT VALUE FROM OGEN.COMMA_TO_TABLE(@P_FACILITY_KEY_CSV)) 
      AND DISCHARGE_DATE IS NULL 
      AND (((@DATEFROM > 0) AND (OGEN.DATEONLY(ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD)) >= @DATEFROM)) 
       OR 
       (@DATEFROM = 0)) 
      AND (((@DATETHRU > 0) AND (OGEN.DATEONLY(ISNULL(SR.USER_DEFINED_DUE_DATE, ISNULL(SR.RECERT_DATE, P.ADMIT_DATE) + @IRENEWALLCYCLEPERIOD)) <= @DATETHRU)) 
       OR 
       (@DATETHRU = 0)) 
      ORDER BY DUE_DATE 
    END 
END 

回答

4

您是否只想在查詢中添加一列即返回的所有記錄的COUNT?

SELECT COUNT(1) OVER() Total, 
     .. <all your other columns> .. 

或者,如果你能在最後返回它作爲SP返回的值,然後

RETURN @@ROWCOUNT 

,假設你已經顯示(中頻/選擇)是你的SP結束。