0

在此处的 sp 中,我试图计算有多少记录符合标准,以便我可以在应用程序的磁贴中显示为总数。但是我没有成功,你能告诉我如何计算以及放在哪里。

(
  @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

1 回答 1

4

您是否只想在查询中添加一个作为返回的所有记录的 COUNT 的附加列?

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

或者,如果您可以将其作为 SP 返回值返回,则

RETURN @@ROWCOUNT

最后,假设您显示的内容(IF/SELECTs)是您的 SP 的结尾。

于 2012-09-27T21:33:00.803 回答