Home Forums SQL Server 2008 T-SQL (SS2K8) Add variable number of rows into a table based on the values in another table (without cursors/while loops) RE: Add variable number of rows into a table based on the values in another table (without cursors/while loops)

  • You could build a tally table in your database an use it directly, or you could use the following as a start for building a dynamic tally table when needed with a query:

    with

    e1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)), -- 10 rows

    e2(n) as (select 1 from e1 a cross join e1 b), -- 100 rows

    eTally(n) as (select row_number() over (order by (select null)) from e2 a cross join e2 b) -- 10,000 rows