相互显示两个用户的角色?

Show roles of two users against each other?

我写了下面的 ABAP 程序,它可以从 AGR_USERS 中获取两个用户的角色,这些角色由用户在 selection 屏幕上指定。

输出屏幕应该有 User id 以及分配给用户的角色。程序对一个用户运行良好,但无法为第二个用户获取角色。

REPORT Z_REFERNCE_USER_PROG.
TYPE-POOLS: slis.  " SLIS contains all the ALV data types
TYPE-POOLS: SLIS,KCDE.
DATA: username(20) TYPE C,
      G_REPID TYPE SY-REPID,
      IT_EVENTS TYPE SLIS_T_EVENT,
      IT_FIELD TYPE SLIS_T_FIELDCAT_ALV,
      WA_FIELD TYPE SLIS_FIELDCAT_ALV,
      IT_SORT TYPE SLIS_T_SORTINFO_ALV.
INITIALIZATION.
G_REPID = SY-REPID.

DATA:BEGIN OF itab OCCURS 0,
       UNAME type AGR_USERS-UNAME,
       AGR_NAME  type AGR_USERS-AGR_NAME,
      UNAME1 type AGR_USERS-UNAME,
       AGR_NAME1  type AGR_USERS-AGR_NAME,
      END of itab.

start-of-selection.
     SELECT-OPTIONS AFF_USER FOR USERNAME NO INTERVALS.
     SELECT-OPTIONS REF_USER FOR USERNAME NO INTERVALS.
end-of-selection.

perform tosql.
perform listshow.
*--------------------------------
* form tosql
*--------------------------------
form tosql.
SELECT DISTINCT AGR_USERS~AGR_NAME    AGR_USERS~UNAME
         INTO corresponding fields of table itab
        FROM AGR_USERS
        where AGR_USERS~UNAME in AFF_USER.

SELECT DISTINCT AGR_USERS~AGR_NAME    AGR_USERS~UNAME
         INTO corresponding fields of table itab
        FROM AGR_USERS
        where AGR_USERS~UNAME in REF_USER.

endform.
*--------------------------------
* form listshow
*--------------------------------
form listshow.
  DEFINE ADD_FIELD.
    WA_FIELD-FIELDNAME = &1.
    WA_FIELD-REPTEXT_DDIC = &2.
    WA_FIELD-NO_ZERO = 'X'.
    APPEND WA_FIELD TO IT_FIELD.
  END-OF-DEFINITION.
     ADD_FIELD 'UNAME' 'Affected user'.
     ADD_FIELD 'AGR_NAME' 'Role'.
     ADD_FIELD 'UNAME1' 'Reference user'.
     ADD_FIELD 'AGR_NAME1' 'Role'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'"
       EXPORTING
            I_CALLBACK_PROGRAM = G_REPID
            I_BACKGROUND_ID   = 'ALV_BACKGROUND'
            IT_FIELDCAT        = IT_FIELD
*            IS_LAYOUT          = GS_LAYOUT
*            IT_SORT            = IT_SORT
            I_SAVE             = 'A'
            IT_EVENTS          = IT_EVENTS[]
       TABLES
            T_OUTTAB           = itab
       EXCEPTIONS
            PROGRAM_ERROR = 1
            OTHERS        = 2.
endform.

输入: 输出:

我想将第一个 SELECT 的所有值放入 table 1 并将第二个 select 的所有值放入 table 2 然后将它们合并到 table 3.我只是想看到相邻的两个用户的角色,顺序无所谓。

SELECT ... INTO 用结果覆盖 table 的内容。所以当你想得到两个SELECTS的结果时,你需要两个tables:

SELECT DISTINCT agr_name, uname
        INTO TABLE @DATA(lt_aff)
        FROM agr_users
        WHERE uname IN @aff_user.

SELECT DISTINCT agr_name, uname
        INTO TABLE @DATA(lt_ref)
        FROM agr_users
        WHERE uname IN @ref_user.

现在如何将这两个结果合二为一table?

问题是您不知道这两个 table 中哪一个会更长。所以我们需要使用一个带有计数器的 WHILE 循环,当计数器小于 either table 的行数时,它会向上计数。然后在该循​​环中,我们需要使用 READ TABLE 获取对应于计数器当前值的两个 table 的行。如果成功,我们需要将其值移动到输出 table itab 的新行,然后我们将其附加到它:

DATA lv_counter TYPE i.
DATA ls_result LIKE LINE OF itab.

lv_counter = 0.

WHILE lv_counter < lines( lt_aff ) OR lv_counter < lines( lt_ref ).
   lv_counter = lv_counter + 1.
   CLEAR ls_result.

   READ TABLE lt_aff INDEX lv_counter INTO DATA(ls_aff).
   IF sy-subrc = 0.
      ls_result-uname = ls_aff-uname.
      ls_result-agr_name = ls_aff-agr_name.
   ENDIF.

   READ TABLE lt_ref INDEX lv_counter INTO DATA(ls_ref).
   IF sy-subrc = 0.
      ls_result-uname1 = ls_ref-uname.
      ls_result-agr_name1 = ls_ref-agr_name.
   ENDIF.

   INSERT ls_result INTO TABLE itab.
ENDWHILE.

实际上您不需要 SELECT-OPTIONS,根据您的任务定义,您只比较两个用户

PARAMETERS: aff_user TYPE xubname DEFAULT 'NEUMANAR',
            ref_user TYPE xubname DEFAULT 'KAJAVESH'.

这是在单个数据库 SELECT 和单个 table:

中的方法
DATA ls_roles TYPE agr_users.

itab-uname = aff_user.
itab-uname1 = ref_user.
SELECT agr_name uname
  FROM agr_users
  INTO (ls_roles-agr_name,ls_roles-uname)
  WHERE uname = aff_user OR uname = ref_user.

  READ TABLE itab ASSIGNING FIELD-SYMBOL(<fs_itab>) WITH KEY agr_name = space.
  IF sy-subrc = 0 AND ls_roles-uname = aff_user.
    <fs_itab>-agr_name = ls_roles-agr_name.
    ELSEIF ls_roles-uname = ref_user.
      UNASSIGN <fs_itab>.
      READ TABLE itab ASSIGNING <fs_itab> WITH KEY agr_name1 = space.
      IF sy-subrc = 0.
        <fs_itab>-agr_name1 = ls_roles-agr_name.
      ENDIF.
  ENDIF.

  IF <fs_itab> IS NOT ASSIGNED.
    IF ls_roles-uname = aff_user.
      itab-agr_name = ls_roles-agr_name.
    ELSE.
     itab-agr_name1 = ls_roles-agr_name.
    ENDIF.
    APPEND itab.
  ENDIF.
ENDSELECT.

它应该做你想做的事