Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 dynamic Union, Iterate, SQL

Author  Topic 

birtprofi
Starting Member

7 Posts

Posted - 2014-08-18 : 10:32:06
Hi,

I need your help. I need to build a dynamic union SQL Statement.
(Or Stored Procedure)

In TableA1 there are the values stored for my SQL Parameters.
For Example:

ID | Name
1 | 16
2 | 23
3 | 30


In TableB1 there are all other values stored. But I need for every row in TableA1 a union

For example:
Select b.* from TableB1 b
join TableA1 a on a.ID = b.ID
where b.ID = 1
union
Select b.* from TableB1
join TableA1 a on a.ID = b.ID
where b.ID = 2


Because TableA1 is dynamic, it is not possible to make a static union-sql. Please help me. Could I do this maybe with: cte WITH ?

Best regards
rf



rf

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-08-18 : 12:22:20
A union is not needed. Just return the matching rows:

Select b.*
from TableB1 b
join TableA1 a on a.ID = b.ID
order by a.ID


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -