Saturday 1 March 2014

Difference Between SY-TABIX and SY-INDEX in SAP ABAP

SY-TABIX, SY-INDEX :


Let’s begin with a test program.


Sample Program:


REPORT ztest_index_tabix.
TYPESBEGIN OF ty_tab,
        
id TYPE i,
       
END OF ty_tab.
DATAi_tab TYPE STANDARD TABLE OF ty_tab INITIAL SIZE 0,
      k_tab 
TYPE ty_tab,
      v_indx 
TYPE i.

k_tab
-id 1.
APPEND k_tab TO i_tab.

k_tab
-id 2.
APPEND k_tab TO i_tab.

k_tab
-id 3.
APPEND k_tab TO i_tab.

LOOP AT i_tab INTO k_tab.
  
WRITE:'Before Read statement'.
  
WRITE:'sy-tabix'sy-tabix,
          
'sy-index'sy-index.

  
READ TABLE i_tab INTO k_tab INDEX 2.
  
WRITE:'After Read statement'.
  
WRITE:'sy-tabix'sy-tabix,
          
'sy-index'sy-index.
  
WRITE:'+++++++++++++++++++++++++++++++++++'.
ENDLOOP.





DO TIMES.
  
WRITE:'Before Read statement'.
  
WRITE:'sy-tabix'sy-tabix,
          
'sy-index'sy-index.

  
READ TABLE i_tab INTO k_tab INDEX 2.
  
WRITE:'After Read statement'.
  
WRITE:'sy-tabix'sy-tabix,
          
'sy-index'sy-index.
  
WRITE:'+++++++++++++++++++++++++++++++++++'.
ENDDO.



Output:



Conclusion: 

In LOOP / ENDLOOP -
SY-INDEX will never work.


SY-TABIX always holds the current executing record’s index for both the LOOP 
and READ statement.







Conclusion:

In DO / ENDDO –
SY-INDEX holds the current executing record’s index only for the DO / WHILE loop and NOT for the READ statement.


SY-TABIX can be used for the READ statement only.




6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, This works only in use of standard internal table but for hashed internal table "SY-TABIX" will not work.

    ReplyDelete
  3. Very good explanation, this is really helpful!!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete