Application Development and Automation 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: 
Read only

LOOP AT dynamic field

0 Likes
2,214

I have an internal table that has a structure something like:


BEGIN OF itab,
  item(10) TYPE c,
  opt1 TYPE c,
  opt2 TYPE c,
  opt3 TYPE c,
END OF itab.

It contains values like:

Item   Opt1 Opt2 Opt3
Item1   X
Item2   X    X
Item3             X

I want to create a method that will loop dynamically on the table for a selected field.

Something like

LOOP AT itab WHERE (field) EQ 'X'.

Where "field" could be Opt1, Opt2 or Opt3.

Is this possible? I have tried using a field symbol

ASSIGN (field) TO <fs_field>.
LOOP AT itab WHERE <fs_field> EQ 'X'.

And I get the error "no component exists with the name <fs_field>.

1 ACCEPTED SOLUTION
Read only

former_member378318
Contributor
0 Likes
949

As an alternative perhaps you can move your dynamic check to within the loop?

LOOP AT itab.

ASSIGN (field) TO <fs_field>.

IF <fs_field> EQ 'X'.

...

ENDIF.

ENDLOOP.

4 REPLIES 4
Read only

former_member378318
Contributor
0 Likes
949

Hi,

The help says this is not possible:

WHERE log_exp

Effect

WHERE can be specified with all table-types. After WHERE, you can specify any logical expression log_exp in which the first operand of any singular comparison is a component of the internal table. For this reason, all logical expressions are possible except for IS ASSIGNED, IS REQUESTED and IS SUPPLIED. <b>Dynamic specification of a component through bracketed character-type data objects is not possible</b>. Loops at sorted tables must have compatible operands of the logical expression. All rows are read for which the logical expression is true.

Not much help I know but atleast you know where you stand.

Read only

former_member378318
Contributor
0 Likes
950

As an alternative perhaps you can move your dynamic check to within the loop?

LOOP AT itab.

ASSIGN (field) TO <fs_field>.

IF <fs_field> EQ 'X'.

...

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
949

Hi

It can't create a dynamic WHERE condition for the LOOP statament, it's possible for the SELECT only, so u need to check your fields inside of LOOP using field-symbols:

LOOP AT itab.

 IF ........ood ide
    FIELD = <...>.
 ENDIF.

 ASSIGN (FIELD) TO <FS_FIELD>.
 CHECK <FS_FIELD> = 'X'.

ENDLOOP.

The problem can be on the performance because you need to loop all records of the internal table, if you use a WHERE clauses the performace will be better.

So if the ITAB can have many records you should decide if it can be a good idea to use the field-symbols.

Max

Read only

0 Likes
949

Yes. That's what I'm going to have to go with.

Not the best performance, but it works.