--建测试表
create table tzs(x varchar(10))

--建存储过程
create proc add_tzs
as
begin
 set nocount on
 declare @mx varchar(10),@i int,@j1 char(1),@j2 char(1)
 select @mx=max(x) from tzs
 
 if @mx is null
 begin
   insert into tzs(x) values('0000001')
   return
 end
 else
 begin
   select @i=2,@mx='0'+substring(@mx,patindex('%[^0]%',@mx),8-patindex('%[^0]%',@mx))
   select @mx=reverse(@mx)
   
   select @j1=substring(@mx,1,1)
   select @j2=case when (@j1>='0' and @j1<='8') or (@j1>='a' and @j1<='y') then char(ascii(@j1)+1)
                   when @j1='9' then 'a'
                   when @j1='z' then '0' end
   select @mx=stuff(@mx,1,1,@j2)
   
   while(@i<=len(@mx))
   begin
     if @j1='z'
     begin
      select @j1=substring(@mx,@i,1)
      select @j2=case when (@j1>='0' and @j1<='8') or (@j1>='a' and @j1<='y') then char(ascii(@j1)+1)
                     when @j1='9' then 'a'
                     when @j1='z' then '0' end
      select @mx=stuff(@mx,@i,1,@j2)
     end
     select @i=@i+1
   end
   
   select @mx=replicate('0',7-len(@mx))+reverse(@mx)
   insert into tzs(x) values(@mx)
   return
 end
end


--测试产生200个编号
exec add_tzs
go 200

--结果
select x from tzs

/*
x
----------
0000001
0000002
0000003
0000004
0000005
0000006
0000007
0000008
0000009
000000a
000000b
000000c
000000d
000000e
000000f
000000g
000000h
000000i
000000j
000000k
000000l
000000m
000000n
000000o
000000p
000000q
000000r
000000s
000000t
000000u
000000v
000000w
000000x
000000y
000000z
0000010
0000011
0000012
0000013
0000014
0000015
0000016
0000017
0000018
0000019
000001a
000001b
000001c
000001d
000001e
000001f
000001g
000001h
000001i
000001j
000001k
000001l
000001m
000001n
000001o
000001p
000001q
000001r
000001s
000001t
000001u
000001v
000001w
000001x
000001y
000001z
0000020
0000021
0000022
0000023
0000024
0000025
0000026
0000027
0000028
0000029
000002a
000002b
000002c
000002d
000002e
000002f
000002g
000002h
000002i
000002j
000002k
000002l
000002m
000002n
000002o
000002p
000002q
000002r
000002s
000002t
000002u
000002v
000002w
000002x
000002y
000002z
0000030
0000031
0000032
0000033
0000034
0000035
0000036
0000037
0000038
0000039
000003a
000003b
000003c
000003d
000003e
000003f
000003g
000003h
000003i
000003j
000003k
000003l
000003m
000003n
000003o
000003p
000003q
000003r
000003s
000003t
000003u
000003v
000003w
000003x
000003y
000003z
0000040
0000041
0000042
0000043
0000044
0000045
0000046
0000047
0000048
0000049
000004a
000004b
000004c
000004d
000004e
000004f
000004g
000004h
000004i
000004j
000004k
000004l
000004m
000004n
000004o
000004p
000004q
000004r
000004s
000004t
000004u
000004v
000004w
000004x
000004y
000004z
0000050
0000051
0000052
0000053
0000054
0000055
0000056
0000057
0000058
0000059
000005a
000005b
000005c
000005d
000005e
000005f
000005g
000005h
000005i
000005j
000005k

(200 row(s) affected)
*/

  

posted on 2013-04-27 10:25  代岳强  阅读(2216)  评论(0编辑  收藏  举报