title: SQL CTE for a numbers table is fast for static value, but slow for table value
body: <p>I'm trying to output the values of a table multiple times, based on a column in that table.</p>
  <p>I tried to use CTE to make a numbers table on the fly:</p>
  <pre><code>WITH cte AS
  (
      SELECT
          ROW_NUMBER() OVER (ORDER BY (select 0)) AS i
      FROM
          sys.columns c1 CROSS JOIN sys.columns c2 CROSS JOIN sys.columns c3
  )
  select *
  from myTable, cte
  WHERE i &lt;= myTable.timesToRepeatColumn
    and myTable.id = '209386'
  </code></pre>
  <p>This SQL seems to take forever to run, so it seems to be trying to run the entire CTE before joining.</p>
  <p>If I replace <code>myTable.timesToRepeatColumn</code> with a static value (say 10000), the query returns virtually instantly.  So it seems to be doing the where i &lt;= before fully cross-joining the CTE's table.</p>
  <p>How can I tell SQL to do the where statement first like it does with a static number?</p>