该如何避免编号重复?
下面代码是用户点击提交后,先取服务器最大签报编号,然后在此基础上加‘1’,再将新的编号记录写入服务器。可发生过两个用户同时点击提交,导致编号相同的问题,请问各位老师是如何避免类似的问题的?
-------------------------------------------------------
*-计算签报编号
DO CASE
CASE ALLTRIM(_WenZ) == "费用"
_shou = "FY"
CASE ALLTRIM(_WenZ) == "差旅"
_shou = "CL"
CASE ALLTRIM(_WenZ) == "假期"
_shou = "JQ"
ENDCASE
SQLIDLEDISCONNECT(_SQLOA)
IF SQLEXEC(_SQLOA,"SELECT top 1 QianBBH FROM oa..LiuShui WHERE WenZ = ?_WenZ ORDER BY QianBBH DESC") = 1 &&取服务器当前文种最大签报编号
SQLIDLEDISCONNECT(_SQLOA)
IF RECCOUNT() = 0
_QianBBH = _shou + LEFT(_SendDate,4) + "-00001"
ELSE
qbbh = VAL(RIGHT(ALLTRIM(sqlresult.QianBBH),5))
qbbh = qbbh + 1
DO CASE
CASE LEN(ALLTRIM(STR(qbbh))) = 1
_QianBBH = _shou + LEFT(_SendDate,4) + "-" + "0000" + ALLTRIM(STR(qbbh))
CASE LEN(ALLTRIM(STR(qbbh))) = 2
_QianBBH = _shou + LEFT(_SendDate,4) + "-" + "000" + ALLTRIM(STR(qbbh))
CASE LEN(ALLTRIM(STR(qbbh))) = 3
_QianBBH = _shou + LEFT(_SendDate,4) + "-" + "00" + ALLTRIM(STR(qbbh))
CASE LEN(ALLTRIM(STR(qbbh))) = 4
_QianBBH = _shou + LEFT(_SendDate,4) + "-" + "0" + ALLTRIM(STR(qbbh))
OTHERWISE
_QianBBH = _shou + LEFT(_SendDate,4) + "-" + ALLTRIM(STR(qbbh))
ENDCASE
ENDIF
ELSE
MESSAGEBOX("取签报号失败。",48,"错误信息")
RETURN
ENDIF
*-根据标题写各分表
DO CASE
*-安全防范费
CASE _BiaoTi = "安全防范费"
SQLIDLEDISCONNECT(_SQLOA)
DO CASE
CASE LEN(ALLTRIM(_QianBBHCG)) = 0 &&如果 _QianBBHCG 长度等于0,说明是新建签报,分表直接写入
IF SQLEXEC(_SQLOA,"INSERT INTO oa..aqff (QianBBH,FeiYLX,GongH,XingM,BuM,FeiYHJ,FangFanNR,ShiY) VALUES (?_QianBBH,?_BiaoTi,?_GongHao,?_XingMing,?_BuM_CLR,?_FB0,?_FB1,?_FB2)") < 1
MESSAGEBOX("写表失败。"+_BiaoTi,48,"错误信息")
RETURN
ENDIF
ENDCASE
ENDCASE
*-写主流水表新建第一条签报信息
IF SQLEXEC(_SQLOA,"INSERT INTO oa..LiuShui .........
[此贴子已经被作者于2018-7-24 12:28编辑过]