Skip to Content
avatar image
Former Member

Dynamic Logical Expressions in ABAP: IF (lt_cond)...

is it possible to have a dynamically constructured logical expression?

I have a condition I need to use in an IF, but I want to avoid hard-coding, so I dynamicallybuild it.

for a simple example:

CONCATENATE ITAB-f1
           'EQ'
           varValue
           INTO lt_cond SEPARATED BY SPACE.
IF (lt_cond).
  WRITE:/ 'Success'.
ENDIF.

Is it possible to do this?

NL

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Mar 29, 2005 at 08:16 PM

    I'm afraind there is no dynamic conditions in IF. There is a statement GENERATE SUBROUTINE POOL. By the way, are you doing some HR programming?

    Message was edited by: Sergei Korolev

    Problems with my mentality.. Sorry for hasty and stupid answer.. Certainly RANGES, SELECT-OPTIONS

    Message was edited by: Sergei Korolev

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 29, 2005 at 08:16 PM

    Try this piece of code....

    <b>DATA: t_t001 TYPE t001 OCCURS 0 WITH HEADER LINE.

    DEFINE my_dynamic_check.

    if &1 &2 &3.

    write:/ 'Success'.

    else.

    write:/ 'Failed'.

    endif.

    END-OF-DEFINITION.

    SELECT * FROM t001 INTO TABLE t_t001.

    LOOP AT t_t001.

    my_dynamic_check t_t001-bukrs eq 'US01'. <u>"US01 can be replaced by a company code in table T001</u>

    ENDLOOP.</b>

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Natasha

      In fact, try this if you have no other options. Below I write a snippet of code. But I wrote it here and did not check in a system. Try optimizing and revising it if neccessary.

      ...
      DATA: BEGIN OF gt_code OCCURS 0 ,
              line(72) TYPE c ,
            END OF gt_code .
      DATA gv_fieldname(30) TYPE c .
      FIELD-SYMBOLS: <f1>, <f2> .
      
      REFRESH gt_code .
      
      gt_code-line = 'REPORT ZTEST.'
      APPEND gt_code .
      gt_code-line = 'FORM test_conditions CHANGING ep_subrc TYPE flag .' .
      APPEND gt_code .
      gt_code-line = 'IF' .
      APPEND gt_code .
      gt_code-line = '(' .
      APPEND gt_code .
      
      
      *----- Begin of Procedure A
      *--Generate field name for the first operand here
      *--e.g. concatenate structure '-' field into gv_fieldname.
      
      ASSIGN (gv_fieldname) TO <f1> . 
      
      *--Generate field name for the second operand here
      *--e.g. concatenate structure '-' field into
      gv_fieldname.
      
      ASSIGN (gv_fieldname) TO <f2> . 
      
      CONCATENATE <f1> '>' <f2> 
                  INTO gt_code-line
                  SEPARATED BY space .
      APPEND gt_code .
      *----- End of Procedure A
      
      
      *--For additional conditions repeat the procedure A
      *--adding an operator and required paranthesises .
      *--e.g. 'AND' here
      
      gt_code-line = 'AND' .
      APPEND gt_code .
      
      *-- Insert Procedure A if needed.
      
      *--End you if statement with a full stop
      gt_code-line = ') .' .
      APPEND gt_code .
      
      ep_subrc = 'X' .
      
      gt_code-line = 'ELSE .' .
      APPEND gt_code .
      
      gt_code-line = 'CLEAR ep_subrc .' .
      APPEND gt_code .
                   
      gt_code-line = 'ENDIF .' .
      APPEND gt_code .
      
      gt_code-line = 'ENDFORM .' .
      APPEND gt_code .
      
      
      *--Now let us generate thesubroutine-pool
      GENERATE SUBROUTINE POOL gt_code 
                               NAME gv_temp_prog_name .
      
      PERFORM test_conditions IN PROGRAM gv_temp_prog_name
              CHANGING gv_passed .
      
      *--Now you have the flag indicating whether your 
      *--condition is met or not
      

      Hope this helps...

      *--Serdar https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]