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

ABAP BI - LOOP / READ - on sorted table

Former Member
0 Likes
1,747


Hi SAP Gurus,

I am beginner on ABAP development.

Could you confirm me that on internal table sorted - loop where exit is similar to read table with key - should be ! isn't it ?

THE SET OF INSTRUCTION ----- LOOP --- WILL DELIVER THE SAME RESULT AS --- READ on sorted table.

        ,   ZFIGL_O1  TYPE SORTED TABLE OF TY_ZFIGL_O1

                      WITH NON-UNIQUE KEY COMP_CODE

                                          FISCVARNT

                                          FISCPER

                                          AC_DOC_NO

CODE 1 ---------------------------------------------------------------------------------------------------

         LOOP AT GTAB-ZFIGL_O1 INTO GWA-ZFIGL_O1
              WHERE COMP_CODE  = <SOURCE_FIELDS>-COMP_CODE  AND
                    FISCVARNT  = <SOURCE_FIELDS>-FISCVARNT  AND
                    FISCPER    = <SOURCE_FIELDS>-FISCPER    AND
                    AC_DOC_NO  = <SOURCE_FIELDS>-AC_DOC_NO  .


           IF GWA-ZFIGL_O1-DOC_NUM IS NOT INITIAL .
             RESULT_FIELDS-DOC_NUM = GWA-ZFIGL_O1-DOC_NUM  .
             EXIT .
           ENDIF .

         ENDLOOP .

CODE 2 ---------------------------------------------------------------------------------------------------

READ TABLE GTAB-ZFIGL_O1 INTO GWA-ZFIGL_O1

WITH KEY COMP_CODE  = <SOURCE_FIELDS>-COMP_CODE  AND

                 FISCVARNT  = <SOURCE_FIELDS>-FISCVARNT  AND

                 FISCPER    = <SOURCE_FIELDS>-FISCPER    AND

                 AC_DOC_NO  = <SOURCE_FIELDS>-AC_DOC_NO  .

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,502

Hi

Both stataments retunrs the first item matching the conditions, it's not important if table is a sorted or standard table

Max

Just to extend Max's reply.

In case of SORTED tables, the LOOP..WHERE & READ TABLE will be optimized (via binary algorithm). Whereas in case of STANDARD tables these operations will result in full table scans (in SAP jargon - sequential table read)

7 REPLIES 7
Read only

Former Member
0 Likes
1,503

Hi

Both stataments retunrs the first item matching the conditions, it's not important if table is a sorted or standard table

Max

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,502

Just to extend Max's reply.

In case of SORTED tables, the LOOP..WHERE & READ TABLE will be optimized (via binary algorithm). Whereas in case of STANDARD tables these operations will result in full table scans (in SAP jargon - sequential table read)

Read only

0 Likes
1,502

Thanks a lot guys !

Last Point when we declare sorted table - is ascending by default. correct ?

TYPES:
   BEGIN OF IT_VBRK,
     VBELN LIKE VBRK-VBELN,
     FKART LIKE VBRK-FKART,
   END OF IT_VBRK.

  DATA : IT_SORTED_VBRK TYPE SORTED TABLE OF IT_VBRK
           WITH NON-UNIQUE KEY FKART.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,502

<ModCapOff>

RTFM

<ModCapOn>

Read the F1 documentation

Read only

0 Likes
1,502

RTFM

Sorted tables are managed by a primary table index. The

entries are listed in this index always according to primary table key. The sort

sequence is ascending and corresponds to the result of the SORT statement

without the specification of additions.

Read only

0 Likes
1,502

hello,

can you please confirm that the Loop statement will not only return the first line matching the condition in the where clause but also all the lines available which matches the where clause.

whereas the read statement will return only the first line having met the where clause.

Read only

0 Likes
1,502

Hi

Yes LOOP returns all records matching where condition, but if you use EXIT into the LOOP the behavior will be the same of READ statament