Skip to Content

Changing table entries where there is a secondary key

TYPES: BEGIN OF role_ty.
 INCLUDE TYPE bapiagr.
TYPES: status TYPE string,
 END OF role_ty.

DATA roles TYPE SORTED TABLE OF role_ty WITH UNIQUE KEY agr_name
             WITH NON-UNIQUE SORTED KEY by_status COMPONENTS status.

LOOP AT roles ASSIGNING FIELD-SYMBOL(<role>) USING KEY by_status 
              WHERE status IS INITIAL.

 CHECK (condition that can't be part of the WHERE clause)
 <role>-status = c_unchanged.
ENDLOOP.


Now, this fails (dumps), because status is part of the secondary key and so protected from changes. One approach is this

        DATA(role) = <role>.
        role-status = c_unchanged.
        DATA unchanged_roles LIKE roles.
        INSERT role INTO TABLE unchanged_roles.
        DELETE TABLE <user_data>-roles FROM <role>.
      ENDLOOP.

      INSERT LINES OF unchanged_roles INTO TABLE roles.

but it seems like too many helper variables. I'm on ABAP 7.4 SP13. Does anyone have any neat ideas how the above could be made more elegant?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Oct 27, 2016 at 09:09 AM

    Hmm, one could loop directly into data(role) instead of field-symbol, saving one line... and that's about it?

    Or something like this?

    TYPES: BEGIN OF role_ty.
     INCLUDE TYPE bapiagr.
    TYPES: status TYPE string,
     END OF role_ty.
    
    DATA roles TYPE SORTED TABLE OF role_ty WITH UNIQUE KEY agr_name
                 WITH NON-UNIQUE SORTED KEY by_status COMPONENTS status.
    
    roles = value #(
      ( agr_name = 'AA'
        agr_text = 'AA text' ")   "Edit in:
        status   = 'c_changed' )  "Ops :)
      ( agr_name = 'BB'
        agr_text = 'BB text' )
    ).
    
    data(roles_temp) = roles .
    LOOP AT roles into data(unchanged_role)
      USING KEY by_status
      WHERE status IS INITIAL.
      unchanged_role-status = |c_unchanged|.
      modify TABLE roles_temp 
        from unchanged_role TRANSPORTING status .
    ENDLOOP .
    roles = roles_temp .
    
    
    Add comment
    10|10000 characters needed characters exceeded