title: Dynamic table design (common lookup table), need a nice query to get the values
body: <p>sql2005 </p>
  
  <p>This is my simplified example:
  (in reality there are 40+ tables in here, I only showed 2)</p>
  
  <p>I got a table called tb_modules, with 3 columns (id, description, tablename as varchar):</p>
  
  <pre><code> 1, UserType, tb_usertype
   2, Religion, tb_religion
  </code></pre>
  
  <p>(Last column is actually the name of a different table)</p>
  
  <p>I got an other table that looks like this:
  tb_value (columns:id, tb_modules_ID, usertype_OR_religion_ID)
  values:</p>
  
  <pre><code> 1111, 1, 45
   1112, 1, 55
   1113, 2, 123
   1114, 2, 234
  </code></pre>
  
  <p>so, I mean 45, 55, 123, 234 are usertype OR religion ID's
  (45, 55 usertype, 123, 234 religion ID`s)</p>
  
  <p>Don't judge, I didn't design the database</p>
  
  <p>Question
  How can I make a select, showing * from tb_value, plus one column
  That one column would be TITLE from the tb_usertype or RELIGIONNAME from the tb_religion table</p>
  
  <p>I would like to make a general thing.
  Was thinking initially about maybe a SQL function that returns a string, but I think I would need dynamic SQL, which is not ok in a function.</p>
  
  <p>Anyone a better idea ?</p>