SQL UNION Operator

Posted on 2013-01-24
Last Modified: 2013-03-04
Hi Expert,

Code is here



----------SQL_MAIN  is main query, @SQL_1 inner join table A, @SQL_2 inner join table B

I want Union  @SQL_2, how to do it?

thanks in advance
Question by:dshi15
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 40

Assisted Solution

lcohan earned 100 total points
ID: 38816210
I suggest you rather use CTE instead of @sql2 - just put all that query or even both in CTE and use them in a UNION afterwards.
here's one with UNION ALL but you can just drop the ALL

more complicated recursive CTE examples here:

Author Comment

ID: 38853449
Why  "use CTE instead of @sql2" ?

in my query string, I pass a lot of variable to the query.
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38896928
as it's not clear (to me) what you want to achieve, can you please show examples of the values of @main, @sql1, @sql2 and the requested end result

Author Comment

ID: 38921092
@main=select * from table A inner join table B

@sql1= @main + " inner join table C"

@sql2= @main + " inner join table D"

Something like that.

I got Union All and result correct, i don't know why use CTE? any better?

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 400 total points
ID: 38921104
that looks fine, actually, in the basics, so it should work
the question is: why you want/need dynamic sql.
with CTE, you would avoid dynamic sql which may give you issues at other levels.

;WITH main AS ( Select * from tableA inner join table B ON ... )
SELECT * from main join table_c on  ...
SELECT * from main join table_d on  ...

Open in new window


Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question