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)

  • Lynn Pettis (3/20/2013)


    Sean Lange (3/20/2013)


    Sure this isn't too hard using a tally table.

    insert #y

    select #x.DocID, t.N

    from #x

    join tally t on t.N <= #x.pages

    select * from #y

    You can read about a tally table here. http://www.sqlservercentral.com/articles/T-SQL/62867/[/url]

    I'm starting to see a pattern with some of the posts. Looks like people are coming here to get their homework done. Anyone else seeing this?

    Yep... I'm thinking the instructor is going to make a mess in his britches when everyone turns in the Tally Table solution. Hopefully, the instructor learns something in the process. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)