Application Development 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: 

Loop at <table> into <fs> where (key).

frank_bork
Participant
0 Kudos

Hi,

we devoloped a new program in SAP Release 7.31 SP14.

Now we have to port this program for the same Client to a different SAP-System with Release 7.00 SP30.

For the above Statement in Release 7.00 we get now the following error message:

In "LOOP...WHERE..." the row type of the table must be statically dfined.

Any idea from which release or SP onwards the above Statement will work or do you know a Workaround for the missing functionality in release 7.00?

Many thanks and best regards

Frank

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

If you mean a dynamic where clauses or condition for an internal table, it should have been introduced from release 7.02

I think you can find out some posts about that

Max

13 REPLIES 13

Former Member
0 Kudos

Hi,

try this

LOOP AT <table> INTO ASSIGNING <fs> WHERE (key).

Marcel

0 Kudos

Hi Marcel,

Thanks for the quick Response.

We tried already. Same error message as we had with the INTO Statement.

Best regards

Frank

0 Kudos

How did you define <table> and <fs>? The problem is <fs> got a different structure from <table>

0 Kudos

Hi Simone,

we are using all field Symbols with type ANY.

As reported it works in 7.31.

Thanks.

Frank

Former Member
0 Kudos

Hi

If you mean a dynamic where clauses or condition for an internal table, it should have been introduced from release 7.02

I think you can find out some posts about that

Max

0 Kudos

Hi Max,

It is not only the where clause, it is also the Loop with field-symbols.

But maybe this is also solved by SAP with release 7.02.

Thanks.

Frank

0 Kudos

If you mean to use a field-symbol not assigned yet, I think so

LOOP AT ITAB INTO <field-symbol> should work if the field-symbol is assigned before the loop, because it would be a "normal" workarea

unfortunately I can work with release 7.00 only, but I keep up to date by SCN

naveen_inuganti2
Active Contributor
0 Kudos

May be you should try assigning structure separately with ASSIGN statement.

assign <structure of table> to <field symbol>.


and then loop table data to <fs>.

Regards,

Naveen

Sandra_Rossi
Active Contributor
0 Kudos

Workaround in 7.0 : if the WHERE expression is not too much complex, you could use the function module FREE_SELECTIONS_WHERE_2_EX to parse the logical expression into a prefixed polish notation, and write a little code like that (to be completed, the NOT is not implemented, ranges are not processed, variables are not handled, etc.) :

REPORT.

PARAMETERS p_logex1 TYPE rsdswhere-line DEFAULT `CARRID = 'AA' or CARRID = 'LH'` LOWER CASE.
PARAMETERS p_logex2 TYPE rsdswhere-line LOWER CASE.

START-OF-SELECTION.
   PERFORM main.

*
FORM main.
   TYPE-POOLS rsds.
   DATA lt_expr TYPE rsds_expr_tab.
   DATA lt_where TYPE rsds_where_tab.
   DATA ls_where TYPE rsdswhere.

   ls_where-line = p_logex1.
   APPEND ls_where TO lt_where.
   ls_where-line = p_logex2.
   APPEND ls_where TO lt_where.

   PERFORM get_expr USING lt_where CHANGING lt_expr.


   DATA lt_sflight TYPE TABLE OF sflight.
   DATA ls_sflight TYPE sflight.
   TYPE-POOLS abap.
   DATA result TYPE abap_bool.

   SELECT * FROM sflight INTO TABLE lt_sflight.

   LOOP AT lt_sflight INTO ls_sflight.
     PERFORM where USING ls_sflight lt_expr CHANGING result.
     CHECK result = abap_true.

     WRITE : / ls_sflight-carrid, ls_sflight-connid, ls_sflight-fldate.
   ENDLOOP.

ENDFORM."
*
FORM get_expr USING it_where TYPE rsds_where_tab CHANGING et_expr type rsds_expr_tab.
   DATA lt_twhere TYPE TABLE OF rsds_where.
   DATA ls_twhere TYPE rsds_where.
   DATA lt_texpr TYPE TABLE OF rsds_expr.
   DATA ls_texpr TYPE rsds_expr.

   ls_twhere-where_tab = it_where.
   ls_twhere-tablename = 'X'.
   APPEND ls_twhere TO lt_twhere.

   CALL FUNCTION 'FREE_SELECTIONS_WHERE_2_EX'
     EXPORTING
       where_clauses        = lt_twhere
     IMPORTING
       expressions          = lt_texpr
     EXCEPTIONS
       incorrect_expression = 1
       OTHERS               = 2.
   IF sy-subrc <> 0.
     RETURN.
   ENDIF.

   READ TABLE lt_texpr INDEX 1 INTO ls_texpr.
   CHECK sy-subrc = 0.
   et_expr = ls_texpr-expr_tab.
ENDFORM."
*
FORM where USING struc t_expr TYPE rsds_expr_tab CHANGING result TYPE abap_bool.
   DATA tabix_initial TYPE i.
   DATA logop_initial TYPE rsdsexpr-logop.
   result = abap_false.
   PERFORM where_recur USING struc logop_initial tabix_initial t_expr CHANGING result.
ENDFORM."
*
FORM where_recur USING struc value(logop) tabix t_expr TYPE rsds_expr_tab CHANGING result TYPE abap_bool.
   DATA ls_expr TYPE rsdsexpr.
   FIELD-SYMBOLS <field> TYPE simple.

   ADD 1 TO tabix.
   READ TABLE t_expr INDEX tabix INTO ls_expr.
   CHECK sy-subrc = 0.

   CASE ls_expr-arity.
     WHEN 0.
       result = abap_false.
       ASSIGN COMPONENT ls_expr-fieldname OF STRUCTURE struc TO <field>.
       CHECK sy-subrc = 0.
       CASE ls_expr-option.
         WHEN 'BT'.
           IF <field> BETWEEN ls_expr-low AND ls_expr-high.
             result = abap_true.
           ENDIF.
         WHEN 'EQ' OR '='.
           IF <field> = ls_expr-low.
             result = abap_true.
           ENDIF.
           "WHEN other options TODO
       ENDCASE.
     WHEN OTHERS.
       CASE ls_expr-logop.
         WHEN 'OR' OR 'AND'.
           logop = ls_expr-logop.
       ENDCASE.
       DATA l_arity TYPE rsdsexpr-arity.
       l_arity = ls_expr-arity.
       WHILE l_arity > 0.
         SUBTRACT 1 FROM l_arity.

         PERFORM where_recur USING struc logop tabix t_expr CHANGING result.

         IF ( logop = 'AND' AND result = abap_false )
               OR ( logop = 'OR' AND result = abap_true ).

           " performance optimization
           WHILE l_arity > 0.
             SUBTRACT 1 FROM l_arity.
             ADD 1 TO tabix.
             READ TABLE t_expr INDEX tabix INTO ls_expr.
             CHECK sy-subrc = 0.
             ADD ls_expr-arity TO l_arity.
           ENDWHILE.
           EXIT.
         ENDIF.
       ENDWHILE.
   ENDCASE.

ENDFORM."

Former Member
0 Kudos

It can also try to generate a subroutine pool:


PARAMETERS: P_WHERE(100) TYPE C.

DATA: PROG TYPE STRING,

           TAB     TYPE STANDARD TABLE OF STRING WITH HEADER LINE,

           MESS  TYPE STRING,

           SID      TYPE STRING.

DATA: IT_T001 TYPE STANDARD TABLE OF T001.

DATA: WA_T001 TYPE T001.



START-OF-SELECTION.

   SELECT * INTO TABLE IT_T001

     FROM T001.

   APPEND 'PROGRAM subpool.'                                                       TO TAB.

   APPEND 'TYPES TY_T001 TYPE STANDARD TABLE OF T001.'     TO TAB.

   APPEND 'FORM DYN_WHERE USING PT_T001 TYPE TY_T001 '  TO TAB.

   APPEND '                     PL_T001 TYPE T001'                                  TO TAB.


   TAB = `  LOOP AT PT_T001 INTO PL_T001 WHERE &.`.

   REPLACE '&' WITH P_WHERE INTO TAB.

   APPEND  TAB.


   APPEND `    EXIT.`                                                                        TO TAB.

   APPEND `  ENDLOOP.`                                                                  TO TAB.

   APPEND `ENDFORM.`                                                                   TO TAB.

  GENERATE SUBROUTINE POOL TAB NAME PROG

              MESSAGE MESS

              SHORTDUMP-ID SID.

   IF SY-SUBRC = 0.

     PERFORM DYN_WHERE IN PROGRAM (PROG)

       USING IT_T001 WA_T001 IF FOUND.

     WRITE: WA_T001-BUKRS, WA_T001-BUTXT.

   ELSEIF SY-SUBRC = 4.

     MESSAGE MESS TYPE 'I'.

   ELSEIF SY-SUBRC = 8.

     MESSAGE SID TYPE 'I'.

   ENDIF.

Of course there are some limits (for example it's possible to generate a maximum of 36 temporary subroutine pools

Max

SuhaSaha
Advisor
Advisor
0 Kudos

Dynamic WHERE clauses were introduced with ABAP 702 Internal Tables in Release 7.0, EhP2 - ABAP Keyword Documentation

Sandra_Rossi
Active Contributor
0 Kudos

If the internal table was initialized from a database table, another alternative is to transfer the dynamic WHERE condition to the SELECT (ABAP documentation - Release notes 6.10 - SELECT - Dynamic WHERE clause)