如何根据结果集里边的数量动态插入不同数目的数据

zblaoshu1979 2014-09-27 08:03:46
现有表结构T1
编号 数量
1001 2
1002 1
1003 4
1004 5

新表T2,根据T1表的数量,把编号放入T2表,T2表只有一个标号字段,数据如下
编号
1001
1001
1002
1003
1003
1003
1003
1004
1004
1004
1004
1004
因为数据量比较大,希望能用速度比较快的方式实现,谢谢大家
...全文
198 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zblaoshu1979 2014-09-28
  • 打赏
  • 举报
回复
引用 8 楼 reenjie 的回复:
[quote=引用 6 楼 zblaoshu1979 的回复:] [quote=引用 3 楼 reenjie 的回复:] 不好意思,手误,再试一下下面的sql。

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号
select * from T2
你好,这个对数据量大的不太适用吧 报错 消息 530,级别 16,状态 1,第 1 行 语句被终止。完成执行语句前已用完最大递归 100。[/quote] 试一下下面的sql

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号 OPTION(MAXRECURSION 0)
select * from T2
[/quote] 这个可以了,谢谢你。
还在加载中灬 2014-09-28
  • 打赏
  • 举报
回复
这个感觉可以不用递归,直接左连接就可以了
INSERT INTO T2([编号])
SELECT A.编号 FROM T1 A
LEFT JOIN master..SPT_VALUES B ON B.type='P' AND B.number>0 AND A.数量>=B.number
LongRui888 2014-09-27
  • 打赏
  • 举报
回复
试试这个:

insert into T2
select 编号
from   T1 t,master..spt_values s  
where s.type = 'P' and s.number >=1 and s.number <= 数量
wtujedp 2014-09-27
  • 打赏
  • 举报
回复
笨办法,游标吧 declare curT cursor for select i,j from t open curT declare @i int declare @j int fetch next from curt into @i,@j while @@FETCH_STATUS=0 begin while @j>0 begin insert into t2 values(@i,1) set @j=@j-1 end fetch next from curt into @i,@j end close curT deallocate curT
xiaodongni 2014-09-27
  • 打赏
  • 举报
回复


with  cte as 
(select 1001 as no,2 as qty  union all
 select 1002 as no,1 as qty  union all
 select 1003 as no,4 as qty  union all
 select 1004 as no,5 as qty  ),
  cte1 as 
  (select no, 1 as qty from cte 
   union all
    select a.no,b.qty+1 from cte as a join cte1 as b on a.no=b.no
     where a.qty>b.qty)
     select no,1 as qty from cte1
     order by no
   
reenjie 2014-09-27
  • 打赏
  • 举报
回复
不好意思,手误,再试一下下面的sql。

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号
select * from T2
zblaoshu1979 2014-09-27
  • 打赏
  • 举报
回复
楼上的,这比实际都多一条
reenjie 2014-09-27
  • 打赏
  • 举报
回复

with cte as 
(
select 编号,数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号
select * from T2
reenjie 2014-09-27
  • 打赏
  • 举报
回复
引用 6 楼 zblaoshu1979 的回复:
[quote=引用 3 楼 reenjie 的回复:] 不好意思,手误,再试一下下面的sql。

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号
select * from T2
你好,这个对数据量大的不太适用吧 报错 消息 530,级别 16,状态 1,第 1 行 语句被终止。完成执行语句前已用完最大递归 100。[/quote] 试一下下面的sql

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号 OPTION(MAXRECURSION 0)
select * from T2
习惯性蹭分 2014-09-27
  • 打赏
  • 举报
回复

;with sel (编号 , 数量) as (
select '1001',   2 union all
select '1002',   1 union all
select '1003',   4 union all
select '1004',   5
)
select a.编号 from sel a
join master..spt_values b on b.type='p' and number>0
and b.number<=a.数量
zblaoshu1979 2014-09-27
  • 打赏
  • 举报
回复
引用 3 楼 reenjie 的回复:
不好意思,手误,再试一下下面的sql。

with cte as 
(
select 编号,数量-1 as 数量 from T1 where 数量>0
union all
select 编号,数量-1 as 数量 from cte where 数量>0
)
insert T2(编号) select 编号 from cte order by 编号
select * from T2
你好,这个对数据量大的不太适用吧 报错 消息 530,级别 16,状态 1,第 1 行 语句被终止。完成执行语句前已用完最大递归 100。

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧