ABAP 740 – LINE_EXISTS to check record in ITAB

By | March 9, 2015 | Concepts | 12,474 | 4

As name suggests, this new predictive function checks if the Line is available in the internal table or not.

Introduction

LINE_EXISTS can be used instead of the Table Expressions.

ABAP_740_List_Exists_Function

Here are the few things to consider:

  • LINE_EXISTS is same as READ TABLE … TRANSPORTING NO FIELDS .. followed by SY-SUBRC CHECK.
  • The call doesn’t return any value. It only checks if the line is there in the table with specified key or not
  • This Function doesn’t set the system field SY-TABIX. So, this can’t be used if you want to use the Parallel Cursor. So, I think you would need use still use the old READ TABLE statement.
  • From SAP Help, This function should be used carefully – you should not use this function to first check if the line is there in the table and than use the table expression to read the table. Instead of that, only the table expression assigning to Field Symbol followed by catching the exception as you did in the article ABAP 740 – Table Expressions to Read & Modify ITAB line, example 5

When to use LINE_EXISTS

As mentioned earlier, it can only replace few instanced of the READ TABLE… TRANSPORTING NO FIELDS which are not using the result value. If you want the result value, use the Table expression instead. If you want to use the SY-TABIX, you would need to use the old READ TABLE statement. I got excite to replace my parallel cursor algorithms but I think I would need to halt on that :(

Lets see few example.

Example – Simple Usage with SY fields behavior

Check if the line exist with the value which exists in the table, followed by the entry which doesn’t exist with equivalent old code.

 
DATA: t_data TYPE STANDARD TABLE OF i.
DATA(lv_value) = 9.
 
DO 10 TIMES.
  APPEND sy-index TO t_data.
ENDDO.
 
*
WRITE: / '>>>> New', lv_value.
IF line_exists( t_data[ table_line = lv_value ] ).
  WRITE: /(20) 'Found', lv_value.
ELSE.
  WRITE: /(20) 'Not Found', lv_value.
ENDIF.
WRITE: /(20) 'SUBRC', sy-subrc,
       /(20) 'TABIX', sy-tabix,
       /(20) 'INDEX', sy-index.
 
*
lv_value = 15.
WRITE: / '>>> New', lv_value.
IF line_exists( t_data[ table_line = lv_value ] ).
  WRITE: /(20) 'Found', lv_value.
ELSE.
  WRITE: /(20) 'Not Found', lv_value.
ENDIF.
WRITE: /(20) 'SUBRC', sy-subrc,
       /(20) 'TABIX', sy-tabix,
       /(20) 'INDEX', sy-index.
 
 
* old
WRITE: /'<<<< OLD', lv_value.
READ TABLE t_data TRANSPORTING NO FIELDS WITH KEY table_line = lv_value.
IF sy-subrc EQ 0.
  WRITE: /(20) 'Found', lv_value.
ELSE.
  WRITE: /(20) 'Not Found', lv_value.
ENDIF.
WRITE: /(20) 'SUBRC', sy-subrc,
       /(20) 'TABIX', sy-tabix,
       /(20) 'INDEX', sy-index.
&#91;/abap_code&#93;
 
As you can notice from the output; SY-TABIX, SY-INDEX, SY-SUBRC doesn't reset from the LINE_EXISTS call.
<div class="post-image"><img src="http://zevolving.com/wp-content/uploads/2015/03/ABAP_740_Line_Exists_Example_1.png" alt="ABAP_740_Line_Exists_Example_1" width="410" height="382" class="alignnone size-full wp-image-2846" /></div>
 
<h3>Example - Simple Negative Usage with SY fields behavior</h3>
Negative check using NOT and NE ..
[abap_code slider="X" title=""]
*
lv_value = 15.
WRITE: / '>>> New', lv_value.
IF NOT line_exists( t_data[ table_line = lv_value ] ).
  WRITE: /(20) 'Not Found', lv_value.
ELSE.
  WRITE: /(20) 'Found', lv_value.
ENDIF.
 
* old
WRITE: /'<<<< OLD', lv_value.
READ TABLE t_data TRANSPORTING NO FIELDS WITH KEY table_line = lv_value.
IF sy-subrc NE 0.
  WRITE: /(20) 'Not Found', lv_value.
ELSE.
  WRITE: /(20) 'Found', lv_value.
ENDIF.
&#91;/abap_code&#93;
 
<div class="post-image"><img src="http://zevolving.com/wp-content/uploads/2015/03/ABAP_740_Line_Exists_Example_2.png" alt="ABAP_740_Line_Exists_Example_2" width="413" height="203" class="alignnone size-full wp-image-2847" /></div>
 
<h3>Example - Compare with Table Expression and LINE_EXISTS and READ</h3>
[abap_code slider="X" title=""]
* New function
lv_value = 15.
WRITE: / '>>> New', lv_value.
IF NOT line_exists( t_data[ table_line = lv_value ] ).
  WRITE: /(20) 'Not Found', lv_value.
ELSE.
  WRITE: /(20) 'Found', lv_value.
ENDIF.
 
 
* New Way
TRY .
    lv_value = 7.
    WRITE: / '>>> New with Table Expression', lv_value.
    DATA(lv_num) = t_data[ table_line = lv_value ].
    WRITE: /(20) 'SUBRC', sy-subrc,
           /(20) 'TABIX', sy-tabix,
           /(20) 'INDEX', sy-index.
  CATCH cx_sy_itab_line_not_found.
    WRITE: / 'Not found'.
ENDTRY.
 
 
* old
WRITE: /'<<<< OLD', lv_value.
READ TABLE t_data TRANSPORTING NO FIELDS WITH KEY table_line = lv_value.
IF sy-subrc NE 0.
  WRITE: /(20) 'Not Found', lv_value.
ELSE.
  WRITE: /(20) 'Found', lv_value.
ENDIF.
&#91;/abap_code&#93;
Even table expression don't reset the SY-TABIX field ... 
<div class="post-image"><img src="http://zevolving.com/wp-content/uploads/2015/03/ABAP_740_Line_Exists_Example_3.png" alt="ABAP_740_Line_Exists_Example_3" width="403" height="285" class="alignnone size-full wp-image-2848" /></div>
 
<h3>Example - Multiple Component check</h3>
LINE_EXISTS works with simple multiple component check as well.
[abap_code slider="X" title=""]
* Multiple Components
TYPES:
  BEGIN OF ty_data,
    kunnr TYPE kunnr,
    name1 TYPE name1,
    ort01 TYPE ort01,
    land1 TYPE land1,
  END   OF ty_data.
TYPES: tt_data TYPE STANDARD TABLE OF ty_data
                                        WITH DEFAULT KEY.
*
DATA(itab_multi_comp) =
  VALUE tt_data( ( kunnr = '123' name1 = 'ABCD' ort01 = 'LV' land1 = 'NV' )
                 ( kunnr = '456' name1 = 'XYZ'  ort01 = 'LA' land1 = 'CA' )
              ).
 
WRITE:  / 'NEW'.
IF line_exists( itab_multi_comp[ kunnr = '456' ] ).
  WRITE: / ' Customer found'.
ELSE.
  WRITE: / ' Customer Not found'.
ENDIF.
 
* old
WRITE:  / 'OLD'.
READ TABLE itab_multi_comp
  TRANSPORTING NO FIELDS
  WITH KEY kunnr = '456'.
IF sy-subrc EQ 0.
  WRITE: / ' Customer found'.
ELSE.
  WRITE: / ' Customer Not found'.
ENDIF.
 

Output

ABAP_740_Line_Exists_Example_4

Example – Chaining of Table Expression within LINE_EXISTS now allowed

 
* Itab Deep
TYPES:
  BEGIN OF ty_alv_data,
    kunnr   TYPE kunnr,
    name1   TYPE name1,
    ort01   TYPE ort01,
    land1   TYPE land1,
    t_color TYPE lvc_t_scol,
  END   OF ty_alv_data.
TYPES: tt_alv_data TYPE STANDARD TABLE OF ty_alv_data
                                        WITH DEFAULT KEY.
 
DATA(itab_alv) =
  VALUE tt_alv_data(
                    "First Row
                    ( kunnr = '123' name1 = 'ABCD'
                        ort01 = 'LV' land1 = 'NV'
                        " color table
                        t_color = VALUE #(
                                          " Color table - First Row
                                           ( fname = 'KUNNR'
                                             color-col = col_negative
                                             color-int = 0
                                             color-inv = 0
                                            )
                                          " Color Table - 2nd Row
                                           ( fname = 'ORT01'
                                             color-col = col_total
                                             color-int = 1
                                             color-inv = 1
                                            )
                                          )
                    )
                    "Second row
                   ( kunnr = '456' name1 = 'XYZ'
                        ort01 = 'LA' land1 = 'CA'
                    )
                 ).
 
if line_exists( itab_alv[ kunnr = '123' ]-t_color[ fname = 'ORT01' ]-color-col = col_background ).
 
endif.  
 

This produces the syntax error that it doesn’t like it ..

ABAP_740_Line_Exists_Example_5

What do you think?

I think I would more use the READ TABLE as generally need to get the access to SY-TABIX for parallel cursor.

Table of Content – ABAP 740 Concepts

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 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 273 articles.

Load comments

4 Comments

  • Martin Pacovsky

    Hi, is it possible to use this together with other key than whole TABLE LINE? Otherwise I see no sense to remember it, because TRANSPORTING NO FIELDS, as you mentioned, has much bigger area of use, and yes, no SY-TABIX is also awkward.

  • Hello Martin,

    Yes, it does work with the Multiple components. I have added the example in the article for that now. I was more focused on the SY fields so didn’t focus on all other cases.

    Also added chaining of the table expression example which is not yet allowed using LINE_EXISTS. May be in next release :)

    Thanks,
    Naimesh Patel

  • Michal

    Hi Naimesh,

    I think the reason why line_exists does not work is stated in the documentation:

    The table expression is only used to check the existence of the specified row. No temporary result is created.

    To modify the deep component, the chaining table expressions can be used together with the exception handling (catching the exception is the same as line_exists = false).

    Check the snippet below:

     
    PROGRAM zchain.
    TYPES:
      BEGIN OF struc_2,
        b_2,
        c_2,
      END OF struc_2,
      tt2 TYPE STANDARD TABLE OF struc_2 WITH DEFAULT KEY,
      BEGIN OF struc_1,
        a_1,
        b_1 TYPE tt2,
      END OF struc_1,
      tt1 TYPE STANDARD TABLE OF struc_1 WITH DEFAULT KEY.
     
    DATA(lt) = VALUE tt1(
                 ( a_1 = 'A' b_1 = VALUE #( ( b_2 = 'B' ) ( b_2 = 'C' ) ) )
                 ( a_1 = 'D' b_1 = VALUE #( ( b_2 = 'E' ) ( b_2 = 'F' ) ) )
               ).
     
    TRY.
        DATA(b_2_value) = 'D'. "change to 'C' to get positive result
        lt[ a_1 = 'A' ]-b_1[ b_2 = b_2_value ]-c_2 = 'X'.
        WRITE:/ 'OK:', lt[ a_1 = 'A' ]-b_1[ b_2 = b_2_value ]-c_2.
      CATCH cx_sy_itab_line_not_found.
        WRITE:/ 'The desired record not found in nested table'.
    ENDTRY.
     

    BR
    Michal

  • Michal

    Oh, I can see that you already described the same here, sorry:
    http://zevolving.com/2015/03/abap-740-table-expressions-read-modif-itab-line/

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

You seem to be new here. Subscribe to stay connected.