ABAP Forum
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Prone to errors: VALUE #( BASE itab ( comp99 = 3 ) )

Sandra_Rossi
Active Contributor
0 Likes
57

This doesn't fill the component COMP99 of all lines of ITAB2:

REPORT zdemo.
TYPES:
  BEGIN OF ty_line,
    a      TYPE i,
    comp99 TYPE i,
  END OF ty_line.
TYPES ty_itab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.
DATA(itab) = VALUE ty_itab( ( a = 1 ) ( a = 2 ) ).
DATA(itab2) = VALUE ty_itab( BASE itab ( comp99 = 3 ) ).

Why? How to do?

Thanks.

Sandra

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
56

I admit that anyone could get into the trap, including me. If I remember well, I had already posted on the same topic in the forum years ago.

When using structures, that works as expected:

DATA(structure) = VALUE ty_line( a = 1 ).
DATA(structure_2) = VALUE ty_line( BASE structure comp99 = 3 ).

Trying the same kind of syntax for an internal table leads to a syntax error:

DATA(itab2) = VALUE ty_itab( BASE itab comp99 = 3 ).

If trying with parentheses, there's no syntax error anymore:

DATA(itab2) = VALUE ty_itab( BASE itab ( comp99 = 3 ) ).

Alas, it has a different meaning:

It means that the internal table is copied as is, and a new line is added with COMP99 = 3 (and the other components have initial values).

Instead, do as follows:

DATA(itab2) = VALUE ty_itab( FOR <line > IN itab 
                             ( VALUE #( BASE <line> comp99 = 3 ) ) ).

or the old way:

DATA(itab2) = itab.
MODIFY itab2 FROM VALUE #( comp99 = 3 ) TRANSPORTING comp99 WHERE comp99 <> 3.

Sandra

View solution in original post

This doesn't fill the component COMP99 of all lines of ITAB2:

REPORT zdemo.
TYPES:
  BEGIN OF ty_line,
    a      TYPE i,
    comp99 TYPE i,
  END OF ty_line.
TYPES ty_itab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.
DATA(itab) = VALUE ty_itab( ( a = 1 ) ( a = 2 ) ).
DATA(itab2) = VALUE ty_itab( BASE itab ( comp99 = 3 ) ).

Why? How to do?

Thanks.

Sandra

1 REPLY 1
Read only

Sandra_Rossi
Active Contributor
57

I admit that anyone could get into the trap, including me. If I remember well, I had already posted on the same topic in the forum years ago.

When using structures, that works as expected:

DATA(structure) = VALUE ty_line( a = 1 ).
DATA(structure_2) = VALUE ty_line( BASE structure comp99 = 3 ).

Trying the same kind of syntax for an internal table leads to a syntax error:

DATA(itab2) = VALUE ty_itab( BASE itab comp99 = 3 ).

If trying with parentheses, there's no syntax error anymore:

DATA(itab2) = VALUE ty_itab( BASE itab ( comp99 = 3 ) ).

Alas, it has a different meaning:

It means that the internal table is copied as is, and a new line is added with COMP99 = 3 (and the other components have initial values).

Instead, do as follows:

DATA(itab2) = VALUE ty_itab( FOR <line > IN itab 
                             ( VALUE #( BASE <line> comp99 = 3 ) ) ).

or the old way:

DATA(itab2) = itab.
MODIFY itab2 FROM VALUE #( comp99 = 3 ) TRANSPORTING comp99 WHERE comp99 <> 3.

Sandra