title: How to join a table where the table name is stored as a column value in another table?
body: <p>To allow multiple payment gateways in my system I have a table of defined payment gateways (id, name, code) where <code>code</code> is the table name (for example 'paypal') containing a specific payment gateway transaction responses.</p>
  
  <p>In my sql server query I want to join the transaction table for each gateway based on the value of this column.</p>
  
  <p>Is this possible? If so, how?</p>
  
  <p>In my query so far I am joining the payment gateway table based on the id of the chosen payment gateway for the specific seller (where <code>[s]</code> is the seller table):</p>
  
  <pre><code>INNER JOIN [payment_gateway] AS [pg] ON [s].[payment_gateway_id] = [pg].[id]
  </code></pre>
  
  <p>What I want to do is something like:</p>
  
  <pre><code>INNER JOIN {{[pg].[code]}} AS [payment_table] ON [payment_table].[order_id] = [order].[id]
  </code></pre>