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
serkurumsal@yandex.com