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

时间:2007-10-06 22:49:00 

'*************************************************
'函数名: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

相关阅读:8个asp生成随机字符的函数

标签:数据库,流水号
0
投稿

猜你喜欢

  • 分支任务:从哪里来,回哪里去

    2009-09-04 18:58:00
  • asp 判断上传文件中是否存在危险代码

    2011-03-17 11:19:00
  • sql随机记录之如何解决每次RND的结果都一样的问题?

    2007-09-11 13:24:00
  • Flash在web客户端的潜在问题

    2009-05-20 12:11:00
  • asp实现将人民币数字改为大写

    2008-05-21 14:08:00
  • asp如何分页显示数据库查询结果?

    2009-11-22 19:23:00
  • position两三事

    2009-02-16 15:23:00
  • asp模板解析类模块(支持if,function,loop及解析缓存)

    2008-08-11 13:06:00
  • js截取字符串的方法

    2008-04-20 17:06:00
  • 教程:MySQL中多表操作和批处理方法

    2009-07-30 08:20:00
  • asp如何定时执行约定的页面?

    2009-11-15 20:17:00
  • CSS样式设计技巧十则

    2008-06-04 12:18:00
  • 恢复master..xp_logattach(log explorer)

    2010-07-01 19:19:00
  • 如何禁止搜索引擎收录你的网站

    2008-10-20 12:57:00
  • 分享css处理浏览器兼容问题上的小技巧

    2008-02-03 14:41:00
  • 从Oracle 表格行列转置说起第1/2页

    2009-09-24 12:51:00
  • 优化Oracle停机时间及数据库恢复

    2010-07-20 12:54:00
  • asp下过滤非法的SQL字符的函数代码

    2011-03-03 11:23:00
  • CSS浏览器兼容问题整理(IE6.0、IE7.0 与FireFox)

    2008-10-27 13:45:00
  • MenuEverywhere 程序图标设计

    2011-08-14 06:57:23
  • asp之家 网络编程 m.aspxhome.com