Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:23:59+00:00 2026-06-04T06:23:59+00:00

Following is inline table valued function CREATE FUNCTION [dbo].[GetDipForDirectClientCharts] ( @FacilityId AS UNIQUEIDENTIFIER ,

  • 0

Following is inline table valued function

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

I have defined two CTE, DirectPatientChartCTE and RemotePatientChartCTE. I don’t want to use union all in case 0 records are returned by RemotePatientChartCTE.

I understand that I can use a where clause in query below union all to check for 0 records in CTE. In that case also second query will be evaluated. I don’t want tables in second query to be scanned in case records don’t exist.

This has been changed from a view to inline TVF since performance with a view was horrible. I cannot use a SP since I have to fill a dynamic temporary table with the results of this TVF. Please suggest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:24:01+00:00Added an answer on June 4, 2026 at 6:24 am

    You can turn your function into a multi statement table valued function. Execute the queries in your CTE’s one at a time and store the result from each query in a table variable each. Then you can check for row count in your second table variable and do the union query if necessary.

    In pseudo code something like this.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CREATE TABLE [dbo].[MembershipModule]( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL, [ParentId] [uniqueidentifier] NULL, [TargetId] [int] NULL,
I have the following function: inline auto iterateSomething(obj & o) { auto iterators =
I'm looking to create an inline function (method) inside my NVelocity template. The solution
i have the following form <form action=#!/search/ method=get style=display:inline; id=hdr_q> <table cellpadding=0 cellspacing=0> <tr><td><input
Given the following table: CREATE TABLE BitValues ( n int ) Is it possible
In the following inline conditionals, one might expect an integer and a double to
The following GCC inline asm is taken from LuaJit's coco library. Can someone provide
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
Given the following code (copied from the attoparsec library) what does the inline pragma
I am trying to assign a form-inline class to the following form in rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.