title: SQLite: dynamic key-value REPLACE using separate table
body: <p>Using <strong>SQLite</strong>, I'd like replace a number of placeholders in a string, but instead of statically providing the key-value pairs, the placeholders and replacement values must come from a table. This question is best illustrated by example:</p>
  
  <pre><code>SELECT
    REPLACE( REPLACE( REPLACE( text_with_placeholders
    , '$NAME', 'Eren')
    , '$TITLE','Mr')
    , '$LEVEL','4th')
  FROM table_with_text
  </code></pre>
  
  <p>In my case though, I have a fairly long list of placeholders, which ideally are dynamic, thus simplifying adding and changing values. So instead of changing the query I'd like to add/update records in a key-value table.</p>
  
  <p>Any smart idea's on how to solve this puzzle? Perhaps for inspiration, my (dead-end) road so far was to use SQLite's nice group_concat() function, this together with a cross join enables you to have the rows with the text and the key-pair values as strings on the same row. But then still you need some sort of loop to go through them, so that won't work.</p>
  
  <pre><code>      SELECT
          REPLACE( REPLACE( REPLACE( text_with_placeholders
          , '$NAME', 'Eren')
          , '$TITLE','Mr')
          , '$LEVEL','4th')
          , kv.keys
          , kv.vals
        FROM table_with_text
  CROSS JOIN ( SELECT GROUP_CONCAT(key) keys, GROUP_CONCAT(val) vals FROM key_val_table ) kv
  </code></pre>
  
  <p><em>I'm aware that the answer to this question probably is Impossible, and that's okay. Still, I'd like to give this a shot given the many brilliant minds at StackOverflow.</em></p>
  
  <p><em>I'm also aware that this is best done in the target application, instead of in the database. So no need to point that out.</em></p>