27,580
社区成员
发帖
与我相关
我的任务
分享
If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([Num] int,[Count2] int,[Code] varchar(10))
Insert tb
Select 92126,200,'100202' union all
Select 94752,100,'100202' union all
Select 19,200,'100201' union all
Select 20,100,'100201'
Go
select
a.num1+b.number num,
a.count2,
a.code
from (
select
num1=case when len(cast(num as varchar))>3 then left(right(cast(num as varchar),4),2) else num end,
num2=case when len(cast(num as varchar))>3 then right(cast(num as varchar),2) else num end,
count2,code
from tb
) a ,master..spt_values b
where b.type='p'
and b.number<=num2-num1
num count2 code
----------- ----------- ----------
21 200 100202
22 200 100202
23 200 100202
24 200 100202
25 200 100202
26 200 100202
47 100 100202
48 100 100202
49 100 100202
50 100 100202
51 100 100202
52 100 100202
19 200 100201
20 100 100201
(14 行受影响)
If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([Num] int,[Count2] int,[Code] varchar(10))
Insert tb
Select 92126,200,'100202' union all
Select 94752,100,'100202' union all
Select 19,200,'100201' union all
Select 20,100,'100201'
Go
select b.number,count2,code from
(select minnum=case when len(num)>4 then left(right(num,4),2) else num end,
maxnum=case when len(num)>4 then right(right(num,4),2) else num end,count2,code from tb)a,master..spt_values b
where b.type='p' and b.number between a.minnum and a.maxnum
--结果:
number count2 code
----------- ----------- ----------
21 200 100202
22 200 100202
23 200 100202
24 200 100202
25 200 100202
26 200 100202
47 100 100202
48 100 100202
49 100 100202
50 100 100202
51 100 100202
52 100 100202
19 200 100201
20 100 100201
If not object_id('tempdb.dbo.#tb') is null
Drop table #tb
If not object_id('tempdb.dbo.#temp') is null
Drop table #temp
Go
Create table #temp([Num] int,[Count2] int,[Code] varchar(10))
Create table #tb([Num] int,[Count2] int,[Code] varchar(10))
--------------------------------------------------------------------
/*以上为构建临时表*/
--------------------------------------------------------------------
Insert #tb
--Select 9212636,200,'100202' union all
Select 92126,200,'100202' union all
Select 94752,100,'100202' union all
Select 19,200,'100201' union all
Select 20,100,'100201'
Go
--Select * from #tb
--------------------------------------------------------------------
/*以上为准备测试用数据*/
--------------------------------------------------------------------
declare @num int
declare @Count2 int
declare @Code varchar(max)
declare @start int --后四位尾数的前两位
declare @end int --后四位尾数的后两位
--------------------------------------------------------------------
/*以上为变量声明*/
--------------------------------------------------------------------
declare cur cursor
for
select Num,Count2,Code from #tb
where Num > 999
open cur
fetch next from cur into @num,@Count2,@Code
while (@@fetch_status =0)
begin
select @start = convert(int,substring(convert(varchar,@num),len(@num)-3,2))
select @end = convert(int,substring(convert(varchar,@num),len(@num)-1,2))
while @start <= @end
begin
insert into #temp
select @start,@Count2,@Code
set @start = @start +1
end
fetch next from cur into @num,@Count2,@Code
end
close cur
deallocate cur
--------------------------------------------------------------------
/*以上为重新构造数据*/
--------------------------------------------------------------------
select * from #temp
union all
select * from #tb where Num <= 999