title: How to match and replace Email template with dynamic query result in Sql Server
body: <p>We are developing email messaging system where our email html templates are saved in database column </p>
  
  <p>i.e. </p>
  
  <blockquote>
    <p>hello user {0}</p>
  </blockquote>
  
  <p>Another column in the same row has dynamic sql related to this template</p>
  
  <p>i.e. </p>
  
  <blockquote>
    <p>Select username as 'To' from userTbl where userid = @userid</p>
  </blockquote>
  
  <p>I want to write a stored proc which can <br />
  - read the dynamic sql<br />
  - execute it and store the result somewhere (may be temp table)<br />
  - then get the html template string and match and replace
    result from the sql statment with the html template</p>
  
  <p>The end result should be</p>
  
  <blockquote>
    <p><code>&lt;html&gt;&lt;body&gt;Hello User username&lt;/body&gt;&lt;/html&gt;</code></p>
  </blockquote>
  
  <p>My question is how do I store the dynamic sql value ? Should I not do this in sql and load everything in c# code and perform search and replace via string builder ?</p>
  
  <p>Any idea or suggetion ?</p>