需求:有多种不同前缀的十四位流水号需要生成,流水号规则为(两位前缀+年+月+日+四位流水号),跨日时流水号从1开始计数。
考虑到有可能出现多人同时生成流水号,造成流水号重复,故对流水号生成进行加锁。
序列号自动分配表
*&---------------------------------------------------------------------*
*& Form set_qydh
*&---------------------------------------------------------------------*
*& 加锁生成流水号
*&---------------------------------------------------------------------*
*& <-- LW_ADD_QYDH
*&---------------------------------------------------------------------*
FORM set_qydh CHANGING lw_add_qydh. "lw_add_qydh是接收生成14位流水号的变量
DATA:lv_type(2) TYPE c VALUE 'SC'. "前缀
DATA:lv_nrlevel TYPE char4. "四位流水号
DATA:ls_zqmt001 TYPE zqmt001. "流水号表
CALL FUNCTION 'ENQUEUE_EZQMT001' "对流水号表加锁
EXPORTING
* MODE_ZQMT001 = 'E'
* MANDT = SY-MANDT
ztype = lv_type "只锁定该类型的流水号,避免影响其他流水号生成
* X_ZTYPE = ' '
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
IF sy-subrc = 0. "加锁成功
SELECT *
FROM zqmt001
INTO TABLE @DATA(lt_zqmt001)
WHERE ztype = @lv_type
AND zyear = @sy-datum+0(4)
AND zmonth = @sy-datum+4(2)
AND zday = @sy-datum+6(2).
READ TABLE lt_zqmt001 ASSIGNING FIELD-SYMBOL(<fs_zqmt001>) INDEX 1.
IF sy-subrc = 0.
"如果在数据行存在,更新流水号再将该流水号存入业务表即可,不必在流水表继续生成
lv_nrlevel = <fs_zqmt001>-zserial + 1.
lv_nrlevel = |{ lv_nrlevel ALPHA = IN }|.
UPDATE zqmt001 SET zserial = lv_nrlevel zyear = sy-datum+0(4) zmonth = sy-datum+4(2) zday = sy-datum+6(2) WHERE ztype = lv_type.
ELSE. "如果在数据行不存在,生成流水号
lv_nrlevel = '0001'.
CLEAR ls_zqmt001.
ls_zqmt001-ztype = lv_type. "型号SC
ls_zqmt001-zserial = lv_nrlevel. "序列号
ls_zqmt001-zyear = sy-datum+0(4). "年
ls_zqmt001-zmonth = sy-datum+4(2)."月
ls_zqmt001-zday = sy-datum+6(2)."日
MODIFY zqmt001 FROM ls_zqmt001.
ENDIF.
IF sy-subrc = 0. "操作成功,释放锁
CALL FUNCTION 'DEQUEUE_EZQMT001'
EXPORTING
* MODE_ZPPT003 = 'E'
* MANDT = SY-MANDT
ztype = lv_type
* X_FEVOR = ' '
.
ENDIF.
lw_add_qydh = lv_type && sy-datum+0(4) && sy-datum+4(2) && sy-datum+6(2) && lv_nrlevel. "最后生成要传给业务表的流水号
ENDIF.
ENDFORM.
对表加锁解锁的函数,在se11处设置好了就能调用
SAP加锁解锁实例