SAP ABAP 用户状态锁定案例

一、前言

项目需求是根据当天及前两天的离职员工信息(假设这是一个定时器任务每天下午5点执行程序,计算前两天的员工工号是为了将5点之后办理离职的员工工号找出来),将这些员工在用户表 USR02 中的锁定状态设置为 “64”,以保证离职员工无法继续使用系统账号。

二、编码

ZHRE008

*&---------------------------------------------------------------------*
*& Report ZHRE008
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zhre008.

INCLUDE zhre008_top. " 数据定义

INCLUDE zhre008_frm." 功能定义

START-OF-SELECTION.

*** 取数方式
  PERFORM frm_get_data.  " 表连接取数
END-OF-SELECTION.

zhre008_top

*&---------------------------------------------------------------------*
*& 包含               ZHRE008_TOP
*&---------------------------------------------------------------------*
TABLES:
  pa0000,usr02.

*** 定义内表 ***
  DATA:
    BEGIN OF gs_item,
      pernr     TYPE pa0000-pernr,     "员工工号
      bname     TYPE usr02-bname,      "用户名
    END OF gs_item.

  DATA:
    gt_item LIKE TABLE OF gs_item.

zhre008_frm

*&---------------------------------------------------------------------*
*& 包含               ZHRE008_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  DATA: lv_count TYPE i. "计数

**** 获取当前日期 ****
  DATA: lv_current_date  TYPE sy-datum, "当前日期变量
        lv_previous_date TYPE sy-datum. "前两天日期变量

**** 计算前两天的时间 ****
  lv_current_date = sy-datum.
  lv_previous_date = lv_current_date - 2.

***PA0000表中获取员工工号(pernr)***
  SELECT
      pernr"员工号
  INTO CORRESPONDING FIELDS OF TABLE gt_item
  FROM pa0000
  WHERE massn = '04'
  AND ( endda = '99991231' OR begda >= lv_previous_date ).

  SORT gt_item BY pernr.

*** 根据获取的员工工号,修改USR02表中的用户锁定状态 ***
  IF gt_item[] IS NOT INITIAL."用于检查变量是否为空或未初始化

    "获取用户名并排除已锁定用户
    SELECT bname INTO TABLE @DATA(lt_bname)
    FROM usr02
    WHERE uflag NOT IN (32,64,128).

    SORT lt_bname BY bname.

  ELSE.
    WRITE: '在PA0000表中找不到离职的员工。'.
  ENDIF.


  LOOP AT gt_item ASSIGNING FIELD-SYMBOL(<gs_item>).
    "类型转换,先将pernr去除20   <gs_item>-bname = <gs_item>-pernr.
    <gs_item>-bname = COND #( WHEN <gs_item>-pernr IS NOT INITIAL THEN <gs_item>-pernr+2(6) ELSE '' ).


    READ TABLE lt_bname INTO DATA(ls_bname) WITH KEY bname = <gs_item>-bname BINARY SEARCH.
    IF sy-subrc = 0.
      "修改锁定状态逻辑
      UPDATE usr02 SET uflag = 64 WHERE bname = ls_bname-bname.
      IF sy-subrc = 0.
        ADD 1 TO lv_count."将变量lv_count的值加1,用于统计更新的记录数
        WRITE: / '员工工号', ls_bname-bname, '已被锁定。'.
        COMMIT WORK."提交之前的事务处理,将进行的数据库操作永久保存
      ENDIF.
    ENDIF.

  ENDLOOP.

  WRITE: / '成功锁定', lv_count, '个离职员工。'.

ENDFORM.

三、效果展示

SAP ABAP 用户状态锁定案例_第1张图片

你可能感兴趣的:(SAP,ABAP,SAP,ABAP)