首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

流水号生成,该如何解决

2013-06-19 
流水号生成数字+字母流水号000000100000020000003000000400000050000006000000700000080000009000000a……00

流水号生成
数字+字母流水号
0000001
0000002
0000003
0000004
0000005
0000006
0000007
0000008
0000009
000000a
……
000000z
0000010
……
000001z
以此类推 数字+字母流水号 流水号
[解决办法]
为什么还搞字母在里面啊?这到时候匹配或者排序很多麻烦的哦
[解决办法]
把0到9,a到z存入到表格,然后按照顺序拼接。
[解决办法]
数字不用说了
字母可以考虑下用ascii()函数,可以返回一个有序的数值
循环自己设计吧

[解决办法]
赞成DBA_Huangzj
如果实在需要数字+字母,不如试试分成2个字段?
[解决办法]
不建议这样弄啊,过程计算麻烦,检索同样麻烦,给服务器增加额外的计算量,不划算的~!
[解决办法]

引用:
赞成DBA_Huangzj
如果实在需要数字+字母,不如试试分成2个字段?

如果按lz的说法,他这个数据的格式是16进制的,不是单纯的数字。
循环还是lz自己来吧
[解决办法]
函数:incNo(@no)
从@no的右端向左循环判断每一位:
  如果是0-8,则该位增1,返回
  如果是9,则该位改为a,返回
  如果是a-y,则该位增1,返回
  如果是z,则该位改为1,继续循环
[解决办法]

--建测试表
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)
*/


[解决办法]
引用:

--建测试表
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)
*/

这个可以结贴了。
[解决办法]
引用:
不建议这样弄啊,过程计算麻烦,检索同样麻烦,给服务器增加额外的计算量,不划算的~!


数据表的字段设计得本身就有缺陷

为什么要是数字+字母?

一、设计缺陷,根本没考虑到搜索的问题

二、假设这个表不是你设计的,分析一下“流水号”字段设计时的初衷(数字+字母),留下字母的目的。
看了你写的一排流水号,是属于流水号不够用?
1、将流水号设置为纯数字,增加位数。在增加、修改、搜索时很方便——搜索出来一目了然。
2、自定义函数,依然为数字+字母,增加、修改、搜索时麻烦。并且,搜索结果是00001和0000a——你看得懂,其他使用者怎么知道?

——过程计算麻烦,检索同样麻烦,给服务器增加额外的计算量
真不知道是哪种方法增加服务器计算量了。
[解决办法]
引用:
Quote: 引用:

不建议这样弄啊,过程计算麻烦,检索同样麻烦,给服务器增加额外的计算量,不划算的~!


数据表的字段设计得本身就有缺陷

为什么要是数字+字母?

一、设计缺陷,根本没考虑到搜索的问题

二、假设这个表不是你设计的,分析一下“流水号”字段设计时的初衷(数字+字母),留下字母的目的。
看了你写的一排流水号,是属于流水号不够用?
1、将流水号设置为纯数字,增加位数。在增加、修改、搜索时很方便——搜索出来一目了然。
2、自定义函数,依然为数字+字母,增加、修改、搜索时麻烦。并且,搜索结果是00001和0000a——你看得懂,其他使用者怎么知道?

——过程计算麻烦,检索同样麻烦,给服务器增加额外的计算量
真不知道是哪种方法增加服务器计算量了。


看使用场合了
这样做,可以用最小的位数表达很大的编码空间(36^7)
像新浪的url缩短功能,就是类似的(10+26+26)^7
[解决办法]
如果不是只用一次,第二次取的流水号不是重复了
引用:

--建测试表
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)


*/


[解决办法]

热点排行