Please below an explanation of the system variables SY-TABIX and SY-INDEX:
SY-TABIX:
SY-TABIX contains the current line in an internal table. That is, it will contain the position of the record we are accessing in an internal table.
SY-TABIX in different scenarios:
Statement ‘APPEND’ will set SY-TABIX to the position of the last table row
Statement ‘COLLECT’ will set SY-TABIX to the position of the appended table row
Statement ‘LOOP’ will set SY-TABIX to the position of the record we are accessing (or current loop record)
Statement ‘READ’ will set SY-TABIX to the position of the record we are reading
Statement ‘SEARCH’ will set SY-TABIX to the position of the table row in which the search string was found
Note: SY-TABIX is set to 0 with hashed tables.
SY-INDEX:
SY-INDEX contains the number of loop passes in DO and WHILE loop.
Code demonstrating different between SY-TABIX and SY-INDEX:
REPORT ZDEMOSYINDEXTABIX.
*Internal tables and structures:
DATA:
lt_bkpf TYPE STANDARD TABLE OF bkpf,
lt_bseg TYPE STANDARD TABLE OF bseg,
lst_bkpf TYPE bkpf,
lst_bseg TYPE bseg.
*---------------------------------------*
*DUMMY RETRIEVAL: *
*---------------------------------------*
*Dummy retrieval for demo purposes.
*Data at header level.
SELECT *
FROM bkpf
INTO TABLE lt_bkpf
UP TO 10 ROWS.
IF sy-subrc EQ 0.
* Sort and delete adjacent duplicates not necessary as
* we are sorting and deleting by primary keys. However, we
* will leave like that to remember that its a best practice
* to sort and delete adjacents duplicates when using for
* all entries.
SORT lt_bkpf BY bukrs belnr gjahr.
DELETE ADJACENT DUPLICATES FROM lt_bkpf COMPARING bukrs belnr gjahr.
* Data at item level.
SELECT *
FROM bseg
INTO TABLE lt_bseg
FOR ALL ENTRIES IN lt_bkpf
WHERE bukrs EQ lt_bkpf-bukrs
AND belnr EQ lt_bkpf-belnr
AND gjahr EQ lt_bkpf-gjahr.
IF sy-subrc EQ 0.
* Sorting not required but its just for
* information purposes.
SORT lt_bseg BY bukrs belnr gjahr.
ENDIF.
ENDIF.
*---------------------------------------*
*SY-TABIX (LOOP): *
*---------------------------------------*
LOOP AT lt_bkpf INTO lst_bkpf.
* Displaying the SY-TABIX when use
* inside a loop.
WRITE: /1 sy-vline,
2(40) 'SY-TABIX (LOOP BKPF):',
41 sy-vline,
50(10) sy-tabix,
61 sy-vline.
WRITE:/1(61) sy-uline.
ENDLOOP.
WRITE:/1 sy-uline.
*---------------------------------------*
*SY-TABIX (READ STATEMENT): *
*---------------------------------------*
LOOP AT lt_bkpf INTO lst_bkpf.
READ TABLE lt_bseg
TRANSPORTING NO FIELDS
WITH KEY bukrs = lst_bkpf-bukrs
belnr = lst_bkpf-belnr
gjahr = lst_bkpf-gjahr
BINARY SEARCH.
IF sy-subrc EQ 0.
* SY-TABIX will returns the position of record
* found in the 'LT_BSEG'.
WRITE: /1 sy-vline,
2(40) 'SY-TABIX (READ BSEG):',
41 sy-vline,
50(10) sy-tabix,
61 sy-vline.
WRITE:/1(61) sy-uline.
ENDIF.
* Clearing necessary items.
CLEAR:
lst_bkpf.
ENDLOOP.
WRITE:/1 sy-uline.
*---------------------------------------*
*SY-INDEX (DO-ENDDO or WHILE-ENDWHILE): *
*---------------------------------------*
DO 5 TIMES.
* Displaying the SY-INDEX when use
* inside a DO loop, same results is
* expected inside a WHILE loop.
WRITE: /1 sy-vline,
2(40) 'SY-INDEX (DO LOOP):',
41 sy-vline,
50(10) sy-index,
61 sy-vline.
WRITE:/1(61) sy-uline.
ENDDO.
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Consultant
Contact : serkurumsal@yandex.com
Social Plugin