Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Changing table entries where there is a secondary key

matt
Active Contributor
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?

1 ACCEPTED SOLUTION

Private_Member_7726
Active Contributor

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 .

3 REPLIES 3

Private_Member_7726
Active Contributor

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 .

0 Kudos

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

0 Kudos

In the end I did your first suggestion.