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.
    
        
            
                
                    
                        
                            
                                | 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 | Name1  | 162  | 233  | 30In TableB1 there are all other values stored. But I need for every row in TableA1 a unionFor example:Select b.* from TableB1 bjoin TableA1 a on a.ID = b.IDwhere b.ID = 1unionSelect b.* from TableB1join TableA1 a on a.ID = b.IDwhere 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 regardsrfrf | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     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 bjoin TableA1 a on a.ID = b.IDorder by a.IDTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |