Skip to Content
2

Changing table entries where there is a secondary key

Oct 27, 2016 at 08:14 AM

112

avatar image
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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Jānis B Oct 27, 2016 at 09:09 AM
1

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 .

Show 2 Share
10 |10000 characters needed characters left characters exceeded

More memory and not sure at all which code would be more efficient...

0

In the end I did your first suggestion.

0