title: Execute a SQL String and insert result into table
body: <p>I have a table </p>
  
  <pre><code>DECLARE @Results TABLE(QueryIndex smallint, FieldValue nvarchar(50))
  </code></pre>
  
  <p>QueryIndex is a loop counter, it will get value from @QueryIndex.
  FieldValue will get value from another SQL String. For some reason, I need to execute SQL string dynamically. </p>
  
  <pre><code>SET @SQL = "SELECT " + @FieldName + " FROM MyView"
              + " WHERE Condition1='" + @Value1 + "'"
              + " AND Condition2=" + CONVERT(nvarchar(12),@Value2)
  </code></pre>
  
  <p>Now I try to insert data into @Results</p>
  
  <pre><code>NSERT INTO @Results(QueryIndex, FieldValue)
  SELECT @QueryIndex, EXEC (@SQL)
  </code></pre>
  
  <p>No surprise, this code won't work. Please provide me solution to insert data into my table. Any methods will be worth trying.
  The result in table should like this:</p>
  
  <pre><code>QueryIndex   FieldName
    1            First
    2            Second
    3            Third
  </code></pre>
  
  <p>Thanks.</p>