SQL Server: Secondary query on empty rowset from first query UNION - cSharp Coder

Latest

cSharp Coder

Sharp Future

Wednesday, July 29, 2020

SQL Server: Secondary query on empty rowset from first query UNION

Here is the best answer in terms of maintenance and performance. 
WITH query1 AS (
    SELECT ...
),
query2 AS (
    SELECT ...
)
SELECT *
    FROM query1
UNION ALL
SELECT *
    FROM query2
    WHERE NOT EXISTS (
        SELECT NULL
        FROM query1
    );
please mark as a answer

1 comment: