asp中用資料庫產生不重複的流水號

來源:互聯網
上載者:User

複製代碼 代碼如下:'*************************************************
'函數名: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.