Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

UNION ALL between Two CTEs

4.86/5 (7 votes)
27 Aug 2010CPOL 31.4K  
Hello, Folks

You may Have tried to Apply Union All betwenn two CTEs.

For Example,
If you Have two Tables Named TableA and TableB

SQL
WITH CTE_TableA
AS
(
SELECT * FROM TableA
)

UNION ALL

WITH CTE_TableA
AS
(
SELECT * FROM TableA
)


Then Above SQL Statement Will be result into Error.

Now To Make Union Of Two CTEs, Follow Below Code Snippets,

SQL
WITH CTE_TableA
AS
(
SELECT * FROM TableA
),CTE_TableB
(
SELECT * FROM TableB
)

SELECT * FROM CTE_TableA
UNION ALL
SELECT * FROM CTE_TableB



Happy Coding !!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)