读书人

求一存储过程一条记录变为多条记录解

发布时间: 2012-02-14 19:19:19 作者: rapoo

求一存储过程,一条记录变为多条记录
表A
name startno endno
aaaa 120 125
bbbb 110 160
插入表B中为
name no
aaaa 120
aaaa 121
... ...
aaaa 125
bbbb 110
bbbb 111
... ...
bbbb 160



[解决办法]
declare tb cursor for select name,startno,endno from from tablea
declare @i int
declare @a1 int
declare @a2 int
declare @name varchar(20)

open tb
fetch next from tb into @name,@a1,@a2
while @@fetch_status=0
begin
set @i = @a1
while (@i <= @a2)
begin
INSERT tableb (name,no) VALUES(@name,@i)
s et @i = @i + 1
end

fetch next from tb into @name,@a1,@a2
end
close tb
deallocate tb
[解决办法]
--如果是100-2000就插不完
--------------------------------那就多产生行数,那就用交叉
Select id=identity(int,0,1) into #t from syscolumns as a,syscolumns as b

读书人网 >SQL Server

热点推荐