Solved

SQL UNION Operator

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

Code is here

SET @SQL_MAIN = @SQL_MAIN + @SQL_1

EXECUTE SP_EXECUTESQL @SQL_MAIN

----------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
0
Comment
Question by:dshi15
  • 2
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:lcohan
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.

http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx
here's one with UNION ALL but you can just drop the ALL

http://stackoverflow.com/questions/8985609/union-all-in-cte

more complicated recursive CTE examples here:
http://stackoverflow.com/questions/3159192/sql-server-2008-cte-recursion
0
 
LVL 1

Author Comment

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

in my query string, I pass a lot of variable to the query.
0
 
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
0
 
LVL 1

Author Comment

by:dshi15
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?

Thanks.
0
 
LVL 143

Accepted Solution

by:
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  ...
UNION ALL
SELECT * from main join table_d on  ...

Open in new window

0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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