Various Ways to Check if ITAB has any record

By | Last Updated on June 4, 2014 | 24,088

Quick overview of various ways to check if there is any record in the Internal Table ITAB using the INITIAL, By using number of lines and more.

Using IS INITIAL

… with IF

 
DATA: itab TYPE STANDARD TABLE OF t100.
 
IF itab IS INITIAL.
  " itab is empty
  " more logic here
ENDIF.
 

… or with CHECK

 
CHECK itab IS INITIAL.
" itab is empty
" more logic here
 

Using LINES

 
IF lines( itab ) EQ 0.
  " itab is empty
  "
ELSE.
  " itab has entries
ENDIF.
 
* Same with CHECK
 

Using DESCRIBE and SY-TFILL

Advertisement

 
DESCRIBE TABLE itab.
IF sy-tfill IS INITIAL.
  " itab is empty
  "
ELSE.
  " itab has entries
ENDIF.
 

* Thanks – Anand, Om Prakash

Using READ

READ TABLE itab TRANSPORTING NO FIELDS INDEX 1.
IF sy-subrc NE 0.
  " itab is empty
  "
ELSE.
  " itab has entries
ENDIF.
 
* Same with check
 

Using LOOP

 
DATA: ls_itab LIKE LINE OF itab.
LOOP AT itab INTO ls_itab FROM 1.
  EXIT.
ENDLOOP.
IF sy-subrc NE 0.
  " itab is empty
  "
ELSE.
  " itab has entries
ENDIF.
 

Is there any other method you use to check if the table has any entries.

* Image source – http://calumhenderson.com/

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Tags

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

10 Comments

  • Anand

    sy-tfill along with describe table.

  • Valeriy

    And what way is the best (in terms of performance)?

  • Nathan Jones

    I just use lines, the reason being you can use it as a variable, which means you can cut down on code lines.

  • Ralf Baumgärtel

    Hi Najmesh,

    I saw in older code this variant:
    DATA:
    lt_t100 TYPE STANDARD TABLE OF t100,
    lv_cnt TYPE i.

    SELECT * FROM t100 INTO TABLE lt_t100
    UP TO 10 ROWS
    WHERE sprsl = sy-langu
    AND arbgb = ‘$$’.

    DESCRIBE TABLE lt_t100 LINES lv_cnt.
    IF lv_cnt = 0.
    ” itab is empty
    ELSE.
    ” itab has entries
    ENDIF.

    But indeed I prefere the lines( itab ) satement.

    Best regards,
    Ralf

  • carsten

    I remember the dodgy

    IF itab[] IS INITIAL

    in former days
    for itabs defined with “occurs xy” or “with header lines”. If it was defnied like this and one forgot to mention the [], only the header was checked, not the body (which could be empty)

    You still need it sometimes in user exits (MV45AFZZ)

  • OM PRAKASH

    Using Describe table statement …
    like ,

    Using Describe table statement

     
     
    DATA: itab TYPE STANDARD TABLE OF t100.
     
    DESCRIBE TABLE ITAB .
     
     IF  SY-TFILL = 0 .
     
    ////// write logic here
     
    ELSEIF .
     
     
  • Steve Oldner

    Never tried sy-tfill, that is a new one for me! The if or check for initial is probably the fastest. Mostly now I code for initial checks after a SELECT – for all entries statement.

  • Thanks Guys. I have updated the code snippet with SY-TFILL.

    @carsten – Yes, you are correct. OCCURS 0 creates lot of confusion now as we can declare using TYPE as well. One of my old article talks about the pain – WITH HEADER LINE – should you use it anymore?.

    @Valeriy – I guess, and as Steve also thinks, IS INITIAL would be faster as it is inbuilt keyword. Haven’t ran any performance comparison on it.

    @Ralf – I have also noticed similar code block. I think SY-TFILL would be better choice, one less variable 🙂

    Thanks Much.

  • Clemens Li

    Just a small hint: You always have a reason to check if an internal table is initial or not. Usually, you want to do something, is the table has records. I like

    Use loop to check if table is initial

     
    LOOP AT ITAB TRANPORTING NO FIELDS.
    * Do what you want to do, i.e.
    SELECT ... INTO ... FROM ... FOR ALL ENTRIES IN itab ...
    EXIT.
    ENDLOOP.
     

    An thank you Naimesh for all the inspiration you give us!

  • John

    Only with ABAP 7.40

     
    IF line_exists( ITAB[ 1 ] ).
    ENDIF.
     

Comments on this Post are now closed. If you have something important to share, you can always contact me.