SAP ABAP Interview Questions (11 - 20)

11. What is the difference between BAPI and RFC?

RFC stands for Remote Function Call, and it is the technical method used for communication between SAP systems or between SAP and non-SAP systems. BAPI stands for Business Application Programming Interface, and it is a standard SAP business method that is usually RFC-enabled.

A simple way to explain it is that RFC is the communication technology, while BAPI is the standard business function exposed through that technology.

12. What is the difference between APPEND and COLLECT?

APPEND simply adds a new row at the end of an internal table. COLLECT adds a row too, but if a row with the same key already exists, it adds the numeric fields instead of inserting a duplicate row. COLLECT is useful when you want quick summarization or grouping of numeric values.

* APPEND example
DATA: itab TYPE TABLE OF i,
      wa   TYPE i.

wa = 10.
APPEND wa TO itab.   "Adds 10
APPEND wa TO itab.   "Adds another 10 (duplicate allowed)
* COLLECT example
TYPES: BEGIN OF ty_data,
         id  TYPE i,
         qty TYPE i,
       END OF ty_data.

DATA: itab TYPE TABLE OF ty_data,
      wa   TYPE ty_data.

wa-id = 1. wa-qty = 10.
COLLECT wa INTO itab.   "Adds new row

wa-id = 1. wa-qty = 5.
COLLECT wa INTO itab.   "Updates same id → qty becomes 15

13. What is READ TABLE with KEY vs INDEX?

READ TABLE ... WITH KEY searches for a row based on field values, so it is useful when you know the business key. READ TABLE ... INDEX reads a row by its row number or position in the internal table. KEY-based reading is more practical in business programs, while INDEX is useful when row order matters.

* READ TABLE ... INDEX example
DATA: itab TYPE TABLE OF i,
      wa   TYPE i.

itab = VALUE #( ( 10 ) ( 20 ) ( 30 ) ).

READ TABLE itab INTO wa INDEX 2.
WRITE wa.   "Output: 20
* READ TABLE ... WITH KEY example
TYPES: BEGIN OF ty_data,
         id   TYPE i,
         name TYPE string,
       END OF ty_data.

DATA: itab TYPE TABLE OF ty_data,
      wa   TYPE ty_data.

itab = VALUE #( ( id = 1 name = 'A' )
                ( id = 2 name = 'B' ) ).

READ TABLE itab INTO wa WITH KEY id = 2.
WRITE wa-name.   "Output: B

14. What is LOOP AT ... WHERE vs READ TABLE?

LOOP AT ... WHERE is used when we want to process multiple rows that meet a condition. READ TABLE is used when we only need one matching row. So the main difference is that LOOP is for many records, while READ is for a single lookup.

* LOOP AT example
LOOP AT it_scarr INTO wa_scarr WHERE currcode = 'USD'.
  WRITE: / wa_scarr-carrid.
ENDLOOP.

* READ TABLE example
READ TABLE it_scarr INTO wa_scarr WITH KEY carrid = 'AA'.

15. What is FOR ALL ENTRIES and its limitations?

FOR ALL ENTRIES is used when we want to fetch database rows based on values stored in an internal table. It behaves somewhat like a dynamic multiple-condition selection and helps reduce repeated SELECTs inside loops.

* FOR ALL ENTRIES example
DATA: it_emp TYPE TABLE OF emp,
      it_db  TYPE TABLE OF zemployee.

SELECT * FROM zemployee INTO TABLE it_db
  FOR ALL ENTRIES IN it_emp
  WHERE emp_id = it_emp-emp_id.

Limitations:

  • If internal table is empty → it fetches ALL records (dangerous).
  • Duplicate entries in internal table can cause duplicate results.
  • Cannot use with some conditions like ORDER BY (limited usage).

16. What happens if FOR ALL ENTRIES table is empty?

If the internal table used in FOR ALL ENTRIES is empty, the system can ignore that condition and fetch a huge number of records from the database. This can slow down the program badly and may even cause memory or runtime issues. That is why developers always check IF itab IS NOT INITIAL before using it.

DATA: it_emp TYPE TABLE OF emp,
      it_db  TYPE TABLE OF zemployee.

* it_emp is EMPTY

SELECT * FROM zemployee INTO TABLE it_db
  FOR ALL ENTRIES IN it_emp
  WHERE emp_id = it_emp-emp_id.

This will fetch all rows from zemployee, which can cause performance issues.

* Best Practice:
IF it_emp IS NOT INITIAL.
  SELECT * FROM zemployee INTO TABLE it_db
    FOR ALL ENTRIES IN it_emp
    WHERE emp_id = it_emp-emp_id.
ENDIF.

17. What is the difference between DELETE ADJACENT DUPLICATES and SORT?

SORT arranges the internal table in a defined order based on one or more fields. DELETE ADJACENT DUPLICATES removes duplicate rows, but only works correctly if table is already sorted.

* DELETE ADJACENT DUPLICATES Example:
DATA: itab TYPE TABLE OF i.

itab = VALUE #( ( 3 ) ( 1 ) ( 3 ) ( 2 ) ( 1 ) ).

SORT itab.  
"Result: 1 1 2 3 3

DELETE ADJACENT DUPLICATES FROM itab.  
"Result: 1 2 3

Without SORT, duplicates may not be removed properly because they are not adjacent.

18. What is Secondary Key in internal table?

Secondary Key is an additional key (besides primary key) used to access internal table data faster using different fields. It helps improve performance when you frequently read data using another field combination.

* READ TABLE using SECONDARY key Example:
TYPES: BEGIN OF ty_data,
         id   TYPE i,
         name TYPE string,
       END OF ty_data.

DATA: itab TYPE SORTED TABLE OF ty_data
      WITH UNIQUE KEY id
      WITH NON-UNIQUE SORTED KEY name_key COMPONENTS name.

READ TABLE itab WITH KEY name = 'A' 
 	USING KEY name_key INTO DATA(wa).

Here, name_key is a secondary key used to quickly search by name.

19. What is Parallel Cursor technique?

The parallel cursor technique is used to improve performance when working with two large internal tables that are related. Instead of using slow nested loops, both tables are sorted and processed in a smarter way.

* Parallel Cursor Example:
SORT itab1 BY id.
SORT itab2 BY id.

DATA: idx TYPE sy-tabix.

LOOP AT itab1 INTO DATA(wa1).
  READ TABLE itab2 INTO DATA(wa2)
       WITH KEY id = wa1-id
       BINARY SEARCH.
  IF sy-subrc = 0.
    idx = sy-tabix.

    LOOP AT itab2 INTO wa2 FROM idx.
      IF wa2-id <> wa1-id.
        EXIT.
      ENDIF.
      WRITE: / wa1-id, wa2-id.
    ENDLOOP.
  ENDIF.
ENDLOOP.

By sorting both tables and using binary search, we can efficiently find matching records without nested loops.

20. What is the performance advantage of Field Symbols?

Field Symbols are like pointers that directly reference memory instead of copying data. This improves performance because it avoids data copying, especially in loops over large internal tables.

* Field Symbols Example:
DATA: itab TYPE TABLE OF i.

itab = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).

LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  <fs> = <fs> * 2.   "Direct update, no copy
ENDLOOP.

Faster than:

* Without Field Symbols (slower due to copying):
LOOP AT itab INTO DATA(wa).
  wa = wa * 2.   "Works on copy
  MODIFY itab FROM wa.
ENDLOOP.