以下是内联表值函数
CREATE FUNCTION [dbo].[GetDipForDirectClientCharts]
(
@FacilityId AS UNIQUEIDENTIFIER ,
@PatientChartStatusCompleteEnum SMALLINT ,
@PatientChartLockingInterval SMALLINT
)
RETURNS TABLE
AS RETURN
(
WITH DirectPatientChartCTE
--Get all patient charts that qualify for dip criteria
AS ( SELECT TOP 500
VisitNumber,PatientChartID
FROM PatientChartCorporate WITH ( READPAST )
WHERE PatientChartCorporate.IsDeleted = 0
AND PatientChartCorporate.IsErroneous = 0
AND PatientChartCorporate.FacilityId = @FacilityId
AND ( DipFileName IS NULL
OR DipFileName = ''
)
AND PatientChartCorporate.ChartStatusID = @PatientChartStatusCompleteEnum
AND DATEDIFF(MINUTE, CompletedOn, GETUTCDATE()) >= +CONVERT(VARCHAR(30), @PatientChartLockingInterval)
AND ( PatientChartCorporate.CompletedOn IS NOT NULL )
),
RemotePatientChartCTE
AS ( SELECT TOP 500
VisitNumber,PatientChartID
FROM PatientCharts WITH ( READPAST )
WHERE PatientCharts.IsDeleted = 0
AND PatientCharts.IsErroneous = 0
AND PatientCharts.FacilityId = @FacilityId
AND ( DipFileName IS NULL
OR DipFileName = ''
)
AND PatientCharts.ChartStatusID = @PatientChartStatusCompleteEnum
AND DATEDIFF(MINUTE, CompletedOn, GETUTCDATE()) >= +CONVERT(VARCHAR(30), @PatientChartLockingInterval)
AND ( PatientCharts.CompletedOn IS NOT NULL )
)
SELECT PatientCharts.VisitNumber ,
PatientChartImages.ImageSequence AS ImageSequence
FROM dbo.PatientChartImagesCorporate AS PatientChartImages WITH ( READPAST )
INNER JOIN DirectPatientChartCTE AS PatientCharts ON PatientChartImages.PatientChartId = PatientCharts.PatientChartId
WHERE Patientchartimages.OnbasedDate IS NULL
UNION ALL
( SELECT PatientCharts.VisitNumber ,
PatientChartImages.ImageSequence AS ImageSequence
FROM dbo.PatientChartImages AS PatientChartImages WITH ( READPAST )
INNER JOIN RemotePatientChartCTE AS PatientCharts ON PatientChartImages.PatientChartId = PatientCharts.PatientChartId
WHERE Patientchartimages.OnbasedDate IS NULL
)
)
我已经定义了两个 CTEDirectPatientChartCTE
和RemotePatientChartCTE
. 我不想使用 union all 以防RemotePatientChartCTE
.
我知道我可以在 union all 下面的查询中使用 where 子句来检查 CTE 中的 0 条记录。在这种情况下,还将评估第二个查询。如果记录不存在,我不希望扫描第二个查询中的表。
这已经从视图更改为内联 TVF,因为视图的性能很糟糕。我不能使用 SP,因为我必须用这个 TVF 的结果填充动态临时表。请建议。