当前位置: 移动技术网 > IT编程>开发语言>Asp > asp中用数据库生成不重复的流水号

asp中用数据库生成不重复的流水号

2017年12月12日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

'*************************************************
'函数名:getmaxorder
'作 用:得到最大序列号
'参 数:fieldname ----在序列号表中的字段名
' tablename ----序列号所在表名
' fieldname ----在表中的字段名
'返回值:字段的最大值
'调用函数:idadd:作用见上
'*************************************************
function getmaxorder(fieldname,tablename,tablefilename)
dim orderno,orderrs,testrs
set testrs=server.createobject("adodb.recordset")
set orderrs=server.createobject("adodb.recordset")
firstno=year(date)&right(("0"&month(date)),2)
ordersql="select * from fieldmaxvalue where fieldname='"&fieldname&"'"
orderrs.open ordersql,conn,3,2
if not orderrs.eof then
orderrs("fieldmaxvalue")=orderrs("fieldmaxvalue")
orderno=orderrs("fieldmaxvalue")
if left(orderno,6)=firstno then
orderno=idadd(orderno)
else
orderno=firstno&"00001"
end if
else
orderrs.addnew
orderrs("fieldname")=fieldname
orderno=firstno&"00001"
end if
testrs.open "select max("&tablefilename&") from "&tablename&" where "&tablefilename&" like '"&firstno&"%'",conn,1,2
if (not testrs.eof) and testrs(0).value>orderno then
orderno=idadd(testrs(0).value)
end if
testrs.close
set testrs=nothing
orderrs("fieldmaxvalue")=orderno
orderrs.update
orderrs.close
set orderrs=nothing
getmaxorder=orderno
end function

'*************************************************
'函数名:idadd
'作 用:用来增加一:比如idadd("5")="6",idadd("l99")="m00",idadd("!")="!1"
' 如果是数字就到9后进位,如果是小写字母到期z后进位
' 如果是大写字母到z后进位,其它在后面加一个1
'参 数:id ----需来增加的数
'返回值:增加后的数
'调用函数:addone 一个数增加一 addone("5")="6",add(9)="0",addone("a")="b",
' addone("z")="a",addone("a")="b",addone("z")="a"
'*************************************************
function addone(first)
dim tempfirst
addone = first
intfirst = asc(first)
if (intfirst >= 48 and intfirst < 57) or (intfirst >= 65 and intfirst < 90) or (intfirst >= 97 and intfirst < 122) then
addone = chr(intfirst + 1)
exit function
end if
if (intfirst = 57) then
addone = "0"
exit function
end if
if (intfirst = 90) then
addone = "a"
exit function
end if
if (intfirst = 122) then
addone = "a"
exit function
end if
end function
function idadd(id)
dim fornt, back, strfind, strbackfind, idlen, tempid
if id="" or isnull(id) then
iddadd=1
exit function
end if
tempid = id
idlen = len(id)
for i = 1 to idlen
fornt = left(id, idlen - i)
back = right(id, i - 1)
strfind = mid(id, idlen + 1 - i, 1)
strbackfind = addone(strfind)
id = fornt & strbackfind & back
if strfind < strbackfind then
exit for
end if
if strfind > strbackfind then
if i = idlen then
id = id & "1"
else
if mid(id, idlen - i, 1) = addone(mid(id, idlen - i, 1)) then
id = fornt & strbackfind & back & "1"
exit for
end if
end if
end if
next
if id = tempid then
id = id & "1"
end if
idadd = id
end function

'调用示例
serviceno=getmaxorder("serviceno","service","serviceno")

'其中保存所有字段流水号最大值的表的结构为:
表名:fieldmaxvalue
字段
id fieldname fieldmaxvalue

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网