Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / SQL
Tip/Trick

UNION ALL between Two CTEs

Rate me:
Please Sign up or sign in to vote.
4.86/5 (7 votes)
27 Aug 2010CPOL 29K   5   5
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)


Written By
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,C#,SQL Query,SSRS,Crystal Reports

Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.

He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

Comments and Discussions

 
GeneralReason for my vote of 5 Good Example. Pin
NMehta8321-Sep-10 19:44
NMehta8321-Sep-10 19:44 
Reason for my vote of 5
Good Example.
GeneralThanks for your comment @linuxjr Pin
Hiren solanki29-Aug-10 20:33
Hiren solanki29-Aug-10 20:33 
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr29-Aug-10 7:12
professionallinuxjr29-Aug-10 7:12 
GeneralThanks, Small but nice Tip/Trick. Pin
Hiren solanki26-Aug-10 19:47
Hiren solanki26-Aug-10 19:47 
GeneralReason for my vote of 5 good one.. Pin
Sandeepkumar Ramani26-Aug-10 19:42
Sandeepkumar Ramani26-Aug-10 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.