zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

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

数据库ASP 生成 重复 中用 流水号
2023-06-13 09:13:41 时间

复制代码代码如下:


"*************************************************
"函数名:getMaxOrder
"作用:得到最大序列号
"参数:fieldName----在序列号表中的字段名
"tableName----序列号所在表名
"fieldName----在表中的字段名
"返回值:字段的最大值
"调用函数:idAdd:作用见上
"*************************************************
functiongetMaxOrder(fieldName,tableName,tableFileName)
dimorderNO,orderRS,testRS
settestRS=Server.CreateObject("ADODB.recordset")
setorderRS=Server.CreateObject("ADODB.recordset")
firstNO=year(date)&right(("0"&month(date)),2)
orderSQL="select*fromfieldMaxValuewherefieldName=""&fieldName&"""
orderRS.openorderSQL,conn,3,2
ifnotorderRS.eofthen
orderRS("fieldMaxValue")=orderRS("fieldMaxValue")
orderNO=orderRS("fieldMaxValue")
ifleft(orderNO,6)=firstNOthen
orderNO=idAdd(orderNO)
else
orderNO=firstNO&"00001"
endif
else
orderRS.addnew
orderRS("fieldName")=fieldName
orderNO=firstNO&"00001"
endif
testRS.open"selectmax("&tableFileName&")from"&tableName&"where"&tableFileName&"like""&firstNO&"%"",conn,1,2
if(nottestRS.eof)andtestRS(0).value>orderNOthen
orderNO=idAdd(testRS(0).value)
endif
testRS.close
settestRS=nothing
orderRS("fieldMaxValue")=orderNO
orderRS.update
orderRS.close
setorderRS=nothing
getMaxOrder=orderNO
endfunction

"*************************************************
"函数名: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"
"*************************************************
FunctionAddOne(first)
Dimtempfirst
AddOne=first
intfirst=Asc(first)
If(intfirst>=48Andintfirst<57)Or(intfirst>=65Andintfirst<90)Or(intfirst>=97Andintfirst<122)Then
AddOne=Chr(intfirst+1)
ExitFunction
EndIf
If(intfirst=57)Then
AddOne="0"
ExitFunction
EndIf
If(intfirst=90)Then
AddOne="A"
ExitFunction
EndIf
If(intfirst=122)Then
AddOne="a"
ExitFunction
EndIf
EndFunction
FunctionidAdd(id)
Dimfornt,back,strFind,strBackFind,idLen,tempid
ifid=""orisNull(id)then
iddAdd=1
exitFunction
endif
tempid=id
idLen=Len(id)
Fori=1ToidLen
fornt=Left(id,idLen-i)
back=Right(id,i-1)
strFind=Mid(id,idLen+1-i,1)
strBackFind=AddOne(strFind)
id=fornt&strBackFind&back
IfstrFind<strBackFindThen
ExitFor
EndIf
IfstrFind>strBackFindThen
Ifi=idLenThen
id=id&"1"
Else
IfMid(id,idLen-i,1)=AddOne(Mid(id,idLen-i,1))Then
id=fornt&strBackFind&back&"1"
ExitFor
EndIf
EndIf
EndIf
Next
Ifid=tempidThen
id=id&"1"
EndIf
idAdd=id
EndFunction

"调用示例
serviceNO=getMaxOrder("serviceNO","service","serviceNO")

"其中保存所有字段流水号最大值的表的结构为:
表名:fieldMaxValue
字段
idfieldNamefieldMaxValue