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

Need Help in Constructing where clause Dynamically

Former Member
0 Likes
877

Dear ABAP Experts,

I have a Database Table which is used for configuring Where clauses like below

SIGN|FIELDS |OPERAND|MIN |MAX

-


1 |FIELD1 |EQ |ABC |

1 |FIELD2 |EQ |123 |

2 |FIELD1 |EQ |DEF |

2 |FIELD2 |EQ |456 |

The Database table will have the data like the one showed above. I Have to construct the "where clause" based on the availabe data as shown above dynamically.

TO understand how to decode AND and OR operation

-


If the SIGN field 2 rows subsequently 1 and 1 - > its AND

If the SIGN field 2 rows subsequently 1 and 2 - > its OR

For the ABOVE data the Where clause can be decoded in this way

( field1 = 'ABC' AND field2 = '123' ) OR ( field1 = DEF' AND field2 = '456' ).

Please let me know how can this be done.

Thanks in Advance.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
845

You could use the AT NEW or AT END statement if you are in a loop, or maybe the ON CHANGE OF.

For example, when the first number in the table changes from 1 to 2, code for the "OR" otherwise code for "AND".

loop at where_clause_tab.

  on change of where_clause_tab-knumv.
* code "OR"
  else.
* code "AND"
  endon.


endloop.

Regards,

Rich Heilman

7 REPLIES 7
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
846

You could use the AT NEW or AT END statement if you are in a loop, or maybe the ON CHANGE OF.

For example, when the first number in the table changes from 1 to 2, code for the "OR" otherwise code for "AND".

loop at where_clause_tab.

  on change of where_clause_tab-knumv.
* code "OR"
  else.
* code "AND"
  endon.


endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
845

Check this FM RSAN_FILL_DYNAMICAL_SELECT.

It takes selection options and converts them to where clause.

It does not cover all the possible operators like NOT and other. You may create custom FM and make it more intelligent.

thanks,

venu

Read only

Former Member
0 Likes
845

Hi, assume all the entries have been selected into ITAB


data: str_where  type string,
      str_temp   type string,
      conca_flag type char1.

sort itab.
loop at itab into lc_itab.
  at new sign.
    conca_flag = 'x'.
  endat.
  if conca_flag = 'x'.
    if str_temp is intial.
      concatenate FIELDS '=' MIN into str_temp. 
    else
      concatenate 'and' FIELDS '=' MIN into str_temp.
    endif.
  endif.
  at end of sign.
    if str_where is initial.
      concatenate '(' str_temp ')' into str_where. 
    else.
      concatenate 'or' '(' str_temp ')' into str_where. 
    endif.
    clear conca_flag.
    clear str_temp .
  endat.
endloop.

The code above is for reference, can't be compiled successfully. Just for reflect an idea.

Hope it will be helpful. Thanks a lot

Message was edited by: zhenglin gu

Read only

Former Member
0 Likes
845

Hi,

Check this document for reference:

https://webphl07.phl.sap.corp/~sapidb/011000358700002805272003E

regards

Aveek

Read only

0 Likes
845

Hello Aveek Ghose ,

I was not able to get this link on? Any help

Read only

Read only

Former Member
0 Likes
845

Thanks all of you for the resoponses.