Current Poll
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
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
sy-tfill along with describe table.
And what way is the best (in terms of performance)?
I just use lines, the reason being you can use it as a variable, which means you can cut down on code lines.
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
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)
Using Describe table statement …
like ,
Using Describe table statement
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.
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
An thank you Naimesh for all the inspiration you give us!
Only with ABAP 7.40