2014 Sep 25 1:41 PM
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 .
2014 Sep 25 1:48 PM
Hi
Both stataments retunrs the first item matching the conditions, it's not important if table is a sorted or standard table
Max
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 .
2014 Sep 25 1:48 PM
Hi
Both stataments retunrs the first item matching the conditions, it's not important if table is a sorted or standard table
Max
2014 Sep 25 3:28 PM
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)
2014 Sep 25 3:56 PM
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.
2014 Sep 25 3:59 PM
2014 Sep 25 4:04 PM
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.
2014 Sep 25 7:29 PM
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.
2014 Sep 26 1:28 AM
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
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |