在这里,我试图计算满足条件的记录数量,以便我可以在应用程序的磁贴中显示总数。但是我没有成功,你能告诉我如何计算以及把它放在哪里。
(
@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
答案 0 :(得分:4)
您是否只希望查询中的其他列是返回的所有记录的COUNT?
SELECT COUNT(1) OVER () Total,
.. <all your other columns> ..
或者如果您可以将其作为SP返回值返回,那么
RETURN @@ROWCOUNT
最后,假设您显示的内容(IF / SELECT)是SP的结尾。