How can SQL create duplicate records?

前端 未结 3 658
再見小時候
再見小時候 2020-12-20 18:22

I was having a look at this question: Getting random value from a SQLite table and it got me wondering if one can duplicate records using SQL. More specifically, is there a

3条回答
  •  时光取名叫无心
    2020-12-20 18:52

    This can be done using a recursive CTE:

    ;WITH CTE_Data as (
        select id=1, name='Anne',[COUNT]=3
        union select id=2, name='Joe',[COUNT]=2
    ),
    CTE_List as (
        select
            id,
            name,
            ind=1
        from CTE_Data
        union all
        select
            l.id,
            l.name,
            ind=ind+1
        from CTE_List l
            join CTE_Data d on l.id = d.id
        where 
            l.ind < d.[count]
    )
    select id,name from CTE_List
    order by id,ind
    

    NOTE: CTE_List is the interesting one, CTE_Data is just the source data for testing.

提交回复
热议问题