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
  • 2
  • 2
LVL 39

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 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BULK LOGGED - log full 9 25
sql query Help 12 52
Authentication error 1 38
triggered use of sp_send_dbmail failure 2 22
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 I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now