I have a table basetable
as given below
id name qty price
2 a 2 20
3 d 3 10
4 b 5 60
i want to insert the rec
You can use a recursive CTE to generate the data:
with cte as (
select bt.id, bt.name, bt.qty, bt.price, 1 as cnt
from basetable bt
union all
select bt.id, bt.name, bt.qty, bt.price, cnt + 1
from cte
where cnt < bt.qty
)
select id, name, 1 as qty, price
from cte;
If you want to put the data in another table, then either use an insert
before the select
or an into
after the select
.
Note: If your quantities get really big, you might have to investigate the MAXRECURSION
option.