1

以下是内联表值函数

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
)
   )

我已经定义了两个 CTEDirectPatientChartCTERemotePatientChartCTE. 我不想使用 union all 以防RemotePatientChartCTE.

我知道我可以在 union all 下面的查询中使用 where 子句来检查 CTE 中的 0 条记录。在这种情况下,还将评估第二个查询。如果记录不存在,我不希望扫描第二个查询中的表。

这已经从视图更改为内联 TVF,因为视图的性能很糟糕。我不能使用 SP,因为我必须用这个 TVF 的结果填充动态临时表。请建议。

4

1 回答 1

2

你可以把你的函数变成一个多语句表值函数。一次执行一个 CTE 中的查询,并将每个查询的结果存储在一个表变量中。然后,您可以检查第二个表变量中的行数,并在必要时进行联合查询。

在这样的伪代码中。

declare @Direct table
(
  PatientChartID int primary key,
  VisitNumber int
)

declare @Remote table
(
  PatientChartID int primary key,
  VisitNumber int
)

insert into @Direct 
select top 500 VisitNumber,PatientChartID
from PatientChartCorporate
--where ....

insert into @Remote
select top 500 VisitNumber,PatientChartID
from PatientChartCorporate
--where ....

if exists(select * from @Remote)
begin
  -- union query here

end
else
begin
  -- non union query here

end
于 2012-05-21T12:40:30.257 回答