--测试数据
create table 表(code char(2),地区 varchar(10))
insert 表 select '01','XXX'
union all select '02','XXX'
union all select '03','XXX'
union all select '04','XXX'
union all select '05','XXX'
union all select '06','XXX'
union all select '07','XXX'
union all select '08','XXX'
union all select '09','yyy'
union all select '10','yyy'
union all select '11','XXX'
union all select '12','XXX'
go
--想来想去,都是要用临时表
select code,地区,id=0 into #t from 表
declare @地区 varchar(10),@i int
set @i=0
update #t set @i=case 地区 when @地区 then @i else @i+1 end
,id=@i,@地区=地区
select 代码范围=min(code)+'-'+max(code),地区=min(地区)
from #t
group by id
go
--写个存储过程处理
create proc p_process
@start int, --开始
@end int --结果
as
insert 表(CODE,地区)
select right(101+a.id+b.id,2),'XXX'
from(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) a,(
select id=0 union all select 10
union all select id=20 union all select 30
union all select id=40 union all select 50
union all select id=60 union all select 70
union all select id=80 union all select 90
) b where a.id+b.id between @start-1 and @end-1
go
--测试
create table 表(code char(2),地区 varchar(10))
go
create proc p_process
@start int, --开始
@end int --结果
as
insert 表(CODE,地区)
select right(101+a.id+b.id,2),'XXX'
from(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) a,(
select id=0 union all select 10
union all select id=20 union all select 30
union all select id=40 union all select 50
union all select id=60 union all select 70
union all select id=80 union all select 90
) b where a.id+b.id between @start-1 and @end-1
go