declare @temptable table1 (title int,title2 int,title3 int) declare @minbianma int ,@bianma varchar(10),@looptimes int,@i int,@1 int,@2 int,@3 int
set @looptimes=50 set @i=0 set @minbianma=5100while @i < @looptimes begin set @bianma=@minbianma+1+@i --过滤不需要的数据 if @bianma = '5102' or @bianma between '5121' and '5132' or @bianma between '5138' and '5143' continue set @1=(select count(*) from table1 where id like @bianma+'%') set @2=(select count(*) from table2 where id like @bianma+'%') set @3=(select count(*) from table3 where id like @bianma+'%') insert into @temptable values (@1, @2, @3) set @i=@i+1 endselect * from @temptable
--哦,对不起,上一个代码有问题,我优化后的代码如下: declare @temptable table1 (title int,title2 int,title3 int) declare @minbianma int ,@bianma varchar(10),@looptimes int,@i int,@1 int,@2 int,@3 int
set @looptimes=50 set @i=0 set @minbianma=5100while @i < @looptimes begin set @bianma=@minbianma+1+@i --过滤不需要的数据 --处理数据5102 if @bianma = '5102' begin set @i=@i+1 --跳到5103 continue end --处理数据5121-5132 if @bianma = '5121' begin set @i=@i+11 --跳到5133 continue end --处理数据5138-5143 if @bianma = '5138' begin set @i=@i+6 --跳到5144 continue end set @1=(select count(*) from table1 where id like @bianma+'%') set @2=(select count(*) from table2 where id like @bianma+'%') set @3=(select count(*) from table3 where id like @bianma+'%') insert into @temptable values (@1, @2, @3) set @i=@i+1 endselect * from @temptable
declare @minbianma int ,@bianma varchar(10),@looptimes int,@i int,@1 int,@2 int,@3 int
set @looptimes=50
set @i=0
set @minbianma=5100while @i < @looptimes
begin
set @bianma=@minbianma+1+@i --过滤不需要的数据
if @bianma = '5102' or @bianma between '5121' and '5132' or @bianma between '5138' and '5143'
continue set @1=(select count(*) from table1 where id like @bianma+'%')
set @2=(select count(*) from table2 where id like @bianma+'%')
set @3=(select count(*) from table3 where id like @bianma+'%') insert into @temptable
values (@1, @2, @3) set @i=@i+1
endselect * from @temptable
declare @temptable table1 (title int,title2 int,title3 int)
declare @minbianma int ,@bianma varchar(10),@looptimes int,@i int,@1 int,@2 int,@3 int
set @looptimes=50
set @i=0
set @minbianma=5100while @i < @looptimes
begin
set @bianma=@minbianma+1+@i --过滤不需要的数据
--处理数据5102
if @bianma = '5102'
begin
set @i=@i+1 --跳到5103
continue
end --处理数据5121-5132
if @bianma = '5121'
begin
set @i=@i+11 --跳到5133
continue
end --处理数据5138-5143
if @bianma = '5138'
begin
set @i=@i+6 --跳到5144
continue
end set @1=(select count(*) from table1 where id like @bianma+'%')
set @2=(select count(*) from table2 where id like @bianma+'%')
set @3=(select count(*) from table3 where id like @bianma+'%') insert into @temptable
values (@1, @2, @3) set @i=@i+1
endselect * from @temptable