Multiple recursive union all selects in a CTE SQL query

user1781272

I am trying to build a query for searching all parent and children rows from a table using a CTE query.

I can either search for parents and return children or search for children and return parents but I can not handle both possibilities in the one query.

I can only complete one of the UNION ALL queries.

Where am I going wrong?

declare @search nvarchar(50)

--set @search = '%Nucleus sub project%';
set @search = '%Nucleus test';

WITH Parent AS
(
    SELECT * 
    FROM tblProjects 
    WHERE ProjNo LIKE @search 
       OR ProjDes LIKE @search

    UNION ALL

    SELECT tblProjects.* 
    FROM tblProjects  
    JOIN Parent ON tblProjects.proID = Parent.ParentProjID

    UNION ALL

    SELECT tblProjects.* 
    FROM tblProjects  
    JOIN Parent ON tblProjects.ParentProjID = Parent.proID 
)
SELECT distinct * 
FROM Parent 
ORDER BY ParentProjID

I get an error :

The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

Gordon Linoff

Try using two different CTEs:

WITH x AS (
    SELECT p.*
    FROM tblProjects p
    WHERE  ProjNo LIKE @search OR ProjDes LIKE @search
   ),
   parents as (
    SELECT * 
    FROM x 
    UNION ALL
    SELECT p.* 
    FROM parents JOIN
         tblProjects p
         ON p.parentid= parents.proID
   ),
   children as (
    SELECT * 
    FROM x 
    UNION ALL
    SELECT p.* 
    FROM children JOIN
         tblProjects p
         ON children.parentid = p.proID
   )
SELECT distinct * 
FROM parents
UNION
SELECT distinct *
FROM children;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple recursive union all selects in a CTE SQL query

From Dev

Recursive SQL CTE query in Pandas?

From Dev

Recursive CTE query in SQL Server

From Dev

SQL Server: issue with UNION ALL and two Selects

From Dev

SQL Server UNION multiple selects in a WHILE statement

From Dev

SQL Server 2008 Join, Union, Multiple Selects?

From Dev

SQL Server recursive CTE query to find all items belonging to all categories and their descendants

From Dev

Grouping [Union All] result from multiple columns SQL query

From Dev

Pandas read_sql query with multiple selects

From Dev

SQL query with multiple criteria and sub selects

From Dev

Sql Query: Union All with Top

From Dev

CTE recursive query

From Dev

SQL Server: combine From lines in Union All Selects

From Dev

SQL Union query - Multiple tables

From Dev

The Recursive CTE to query all ancestors of a Parent-Child table is slow

From Dev

Recursive query with CTE - Concatenate all fields of child elements and add to parent

From Dev

How to turn a multiple self Join query into a recursive CTE

From Dev

UNION SELECTs - loop? or single query?

From Dev

UNION SELECTs - loop? or single query?

From Dev

Recursive CTE in SQL

From Dev

SQL group with Recursive CTE

From Dev

SQL complicated recursive CTE

From Dev

SQL: Optimizing Recursive CTE

From Dev

T-SQL Recursive CTE with Multiple Parent Items

From Dev

recursive cte - mark all leafs

From Dev

Collation conflict in SQL Union All Query

From Dev

Collation conflict in SQL Union All Query

From Dev

LINQ equivalent of my SQL Query for UNION ALL

From Dev

SQL subquery combine with UNION ALL query