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

How to write dynamic where condition in loop ?

vinayad
Participant
0 Likes
6,451

Is it possible to achieve this ?

field-symbols : <i_itab> TYPE table ,

<wa_tab> type any.

Loop at <i_itab> assigning <wa_tab> where (dynamic condition).

I am trying to replace value, if the condition satisfies it will change but i am unable to satisfy the condition here.

- thank you !

Is it possible to achieve this ?

field-symbols : <i_itab> TYPE table ,

<wa_tab> type any.

Loop at <i_itab> assigning <wa_tab> where (dynamic condition).

I am trying to replace value, if the condition satisfies it will change but i am unable to satisfy the condition here.

- thank you !

8 REPLIES 8
Read only

tamitdassharma
Active Participant
0 Likes
5,669

Hello vinayad,

Could you put in some more information about how your dynamic condition will be formed?

Read only

Sandra_Rossi
Active Contributor
Read only

0 Likes
5,669

Hi Sandra Rossi

When I tried this below way,

DATA dref TYPE REF TO i.

DATA :BEGIN OF line,
col1 TYPE i,
col2 TYPE i,
END OF line.

dref = REF #( num ).

LOOP at <i_itab> INTO line FROM 10 TO 25 WHERE col2 > dref->*.

I am getting error, The field REF is unknown.

Read only

0 Likes
5,669

It's a completely different question. "dref = REF #( num )" works only from ABAP 7.40 SP 2.

Read only

Former Member
0 Likes
5,669

Kindly try with range. Declare data type as range and then loop that dyanmic query into work area.

Read only

DoanManhQuynh
Active Contributor
0 Likes
5,669

where is the condition?

Read only

vinayad
Participant
0 Likes
5,669

Hi Tamit Kumar Das Sharma

data cond type string.

Loop at <i_itab> assigning <wa_tab> where (cond).

-Thanks.

Read only

Nawanandana
Active Contributor
0 Likes
5,669

Hi,

CASE gv_cdvue.
  WHEN 'L'.
    lv_condi = 'dcmdr GE gv_daybf   OR dcmdr LE gv_daybf   '.
  WHEN 'C'.
    lv_condi = 'dcldr GE gv_daybf   OR dcldr LE gv_daybf   '.
  WHEN 'E'.
  WHEN OTHERS.


ENDCASE.


  LOOP AT gt_plcad INTO ls_plcad  WHERE (lv_condi).
    ....
  ENDLOOP.

https://answers.sap.com/questions/5712863/dynamic-where-clause-for-loop-at-internal-table.html

DATA: lt_spfli TYPE STANDARD TABLE OF spfli,
       lv_where TYPE string,
       lv_fld1  TYPE string value 'CARRID',
       lv_fld2  TYPE string VALUE 'AIRPTO',
       lv_carrid TYPE s_carr_id VALUE 'AA',
       lv_airpt  TYPE s_toairp VALUE 'JFK',
       lv_sing_quote TYPE string VALUE '''',
       lv_stub   TYPE string,
       lv_stub2   TYPE string.
FIELD-SYMBOLS: <fs_spfli> TYPE spfli.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_spfli FROM spfli.
"To add the single quote around value. E.g.  AA => 'AA', JFK => 'JFK'
CONCATENATE lv_sing_quote lv_carrid lv_sing_quote INTO lv_stub.
CONCATENATE lv_sing_quote lv_airpt lv_sing_quote INTO lv_stub2.
"Where clause without AND
CONCATENATE lv_fld1 '=' lv_stub INTO lv_where SEPARATED BY space.
ULINE.
LOOP AT lt_spfli ASSIGNING <fs_spfli> WHERE (lv_where).
   WRITE: / <fs_spfli>-carrid, <fs_spfli>-airpto.
ENDLOOP.
ULINE.
NEW-LINE.
NEW-LINE.
CLEAR lv_where.
"Where clause with AND
CONCATENATE lv_fld1 '=' lv_stub 'AND' lv_fld2 '=' lv_stub2 INTO lv_where SEPARATED BY SPACE.
ULINE.
LOOP AT lt_spfli ASSIGNING <fs_spfli> WHERE (lv_where).
   WRITE: / <fs_spfli>-carrid, <fs_spfli>-airpto.
ENDLOOP.
ULINE.