title: Joining a query result to a distinct dynamic query in SQL
body: <p>I've been trying all day long to merge a simple query result of a single row to another dynamic query with a single row. To briefly explain what I've got is as follows:</p>
  
  <p>I have a query as a result of a complex SELECT that returns a single row of two columns:</p>
  
  <pre><code>HistoryDate |TotalMarketValue
  2014-08-31   1195687865.20
  </code></pre>
  
  <p>Then I have another dynamic query which returns a single row with dynamic number of columns, where I do not know the name nor number of columns that would be returned, which I execute something like this:</p>
  
  <pre><code>exec sp_executesql @query
  </code></pre>
  
  <p>So basically what I need to do is something like:</p>
  
  <pre><code>SELECT HistoryDate, SUM(TotalMarketValue) FROM MyComplexTable, exec sp_executesql @query
  </code></pre>
  
  <p>Obviously the above syntax is wrong as the exec part will not allow me to do a CROSS JOIN. I've tried various other ways like attempting to populate the dynamic query results to a temporary table, but then again it doesn't allow me to create a table without specifying the number of columns. I'm just missing that point.</p>
  
  <p>Can somebody let me know how I could get the dynamic query as part of the merged query?</p>