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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
*&--------------------------------------------------------------------&* *& Program Description: &* *& ----------------------- &* *& This demo program will demonstrate the difference between SY-TABIX &* *& and SY-INDEX. &* *& &* *& Author: ABAPCOOKBOOK &* *& Website: www.abapcookbook.com &* ************************************************************************ 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. |
The results: