简体   繁体   中英

Inserting Multiple of same records into SQL temp table based on value in column

So I have table with the following records:

源表

I want to create a script to iteratively look at the Cnt_Repeat column and insert that same record in a temp table X times depending on the value in Cnt_Repeat so it would look like the following table:

结果表

One method supported by most databases is the use of recursive CTEs. The exact syntax might vary, but the idea is:

with cte as (
      select loannum, document, cnt_repeat, 1 as lev
      from t
      union all
      select loannum, document, cnt_repeat, lev + 1
      from cte
      where lev < cnt_repeat
     )
select loannum, document, cnt_repeat
from cte;
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM