一个表中有数据
name value
A 1
B 3
C 2
生成另外一个表数据为:
name value
A 1
B 3
B 3
B 3
C 2
C 2
也就是第一个表中value 值为几就生成几条重复的数据
name value
A 1
B 3
C 2
生成另外一个表数据为:
name value
A 1
B 3
B 3
B 3
C 2
C 2
也就是第一个表中value 值为几就生成几条重复的数据
调试欢乐多
insert test
select 'A', 1 union
select 'B', 3 union
select 'C', 2select top 10 id into #T from sysobjectsselect a.* into # from test a left join #T b on b.id <=a.[value]select * from #
drop table test,#,#tname value
---------- -----------
A 1
B 3
B 3
B 3
C 2
C 2(所影响的行数为 6 行)
declare @name varchar(8) , @value int
declare @n int
set @n = 1
declare cur1 cursor select naem,value from t where 1 = 1
open cur1
fetch next from cur1 into @name, @value
while @@fetch_status = 0
begin
while @n < @value
begin
insert into t
select @name , @value
set @n = @n + 1
end
set @n = 1
fetch next from cur1 into @name , @value
end
deallocate cur
declare @t1 table (name char(5),value int)
set nocount on
insert @t values('A', 1)
insert @t values('B', 3)
insert @t values('C', 2)
declare @i int
select @i=max(value) from @twhile @i>0
begin
insert @t1 select name,value from @t where @i<=value
set @i=@i-1
end
set nocount off
select * from @t1 order by namename value
----- -----------
A 1
B 3
B 3
B 3
C 2
C 2(所影响的行数为 6 行)