ABAP – Select * (all columns) and INNER JOIN

I fyou want to select all fields of one table and just few fields of another (joined) table, you have to specify all required fields manually. It is NOT possible to write SELECT TAB_1~* TAB_2~FIELD_1 TAB_2~FIELD_2…. In this article I’ll show how to make this thing easier 🙂

The selection of all fields (SELECT tabname.*) will be done with the following helper method/form:

TYPES: tt_fields TYPE TABLE OF char300.
FORM add_tabfields_for_selection USING    iv_tabname 
                                 CHANGING ct_fields TYPE tt_fields.

DATA:
    lt_dfies   TYPE dfies_tab,
    lv_tabname TYPE ddobjname.

  lv_tabname = iv_tabname.

  CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      tabname   = lv_tabname
    TABLES
      dfies_tab = lt_dfies.

* Append each field as line of TABNAME~FIELDNAME of the itab
  LOOP AT lt_dfies ASSIGNING FIELD-SYMBOL(<ls_dfies>).
    APPEND |{ <ls_dfies>-tabname }~{ <ls_dfies>-fieldname }| TO ct_fields.
  ENDLOOP.
ENDFORM.

The real usage of this form might be as in the following code snippet:

TYPES:
  BEGIN OF ts_data.
    INCLUDE TYPE mara.
TYPES:
    maktx   TYPE maktx,
  END OF ts_data,
  tt_data TYPE TABLE OF ts_data.

DATA:
  lt_fields TYPE tt_fields,
  lt_data   TYPE tt_data.
  
* Add all fields of selected DB table to the SQL selection
  PERFORM add_tabfields_for_selection USING    'MARA'
                                      CHANGING lt_fields.

* Add subset of fields from MAKT (mat. description)
  APPEND 'MAKT~MAKTX' TO lt_fields.
* ...another fields

  SELECT (lt_fields)
    INTO CORRESPONDING FIELDS OF TABLE lt_data
    FROM mara
      INNER JOIN makt ON mara~matnr = makt~matnr
    UP TO 5000 ROWS
    WHERE makt~spras = sy-langu.

2 thoughts on “ABAP – Select * (all columns) and INNER JOIN

    • Hi,
      thanks a lot for this information – Our company does not currently have this version and service pack installed, therefore I was not able to use this form of the SELECT command – but anyway, it’s good to know there’s finally this feature in SAP 🙂

Leave a Reply