ABAP Select data from SAP table BAPISDHDST into internal table
Get Example source ABAP code based on a different SAP table
Below is a number of ABAP code snippets to demonstrate how to select data from SAP BAPISDHDST table and store it within an internal table, including using the newer @DATA inline declaration methods. It also shows you various ways to process this data using ABAP work area, inline declaration or field symbols including executing all the relevant CONVERSION_EXIT routines specific to BAPISDHDST. See here for more generic Select statement tips.
Sometimes data within SAP is stored within the database table in a different format to what it is displayed to the user. These input/output conversation FM routines are what translates the data between the two formats.
There is also a full declaration of the BAPISDHDST table where each field has a char/string type for you to simply copy and paste. This allows you to use processing that is only available to these field types such as the CONCATENATE statement.
DATA: IT_BAPISDHDST TYPE STANDARD TABLE OF BAPISDHDST, WA_BAPISDHDST TYPE BAPISDHDST, GD_STR TYPE STRING. DATA: lo_typedescr type REF TO cl_abap_typedescr. DATA: lv_fieldname type fieldname. FIELD-SYMBOLS: <FIELD> TYPE any. FIELD-SYMBOLS: <BAPISDHDST> TYPE BAPISDHDST. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_BAPISDHDST. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM BAPISDHDST INTO TABLE IT_BAPISDHDST. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM BAPISDHDST * INTO TABLE @DATA(IT_BAPISDHDST2). *--Further methods of using ABAP code to select data from SAP database tables *You can also declare the header/work area using the in-line DATA declaration method READ TABLE IT_BAPISDHDST INDEX 1 INTO DATA(WA_BAPISDHDST2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_BAPISDHDST ASSIGNING <BAPISDHDST>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPISDHDST>-OPERATION = 1.
<BAPISDHDST>-SD_DOC = 1.
<BAPISDHDST>-REFDOCHDST = 1.
<BAPISDHDST>-TOTREFSTAT = 1.
<BAPISDHDST>-CONFIRSTAT = 1.
ENDLOOP. LOOP AT IT_BAPISDHDST INTO WA_BAPISDHDST. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_BAPISDHDST-DELIV_STAT, sy-vline,
WA_BAPISDHDST-DLV_STAT_H, sy-vline,
WA_BAPISDHDST-TOTGOODSMV, sy-vline,
WA_BAPISDHDST-BILLSTATUS, sy-vline,
WA_BAPISDHDST-BILLINGSTA, sy-vline,
WA_BAPISDHDST-ACCOUNTSTA, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPISDHDST you want to display... WRITE:/ sy-uline. * Aternatively use generic code to Write field values (and NAME) to screen report DO. ASSIGN COMPONENT sy-index OF STRUCTURE wa_BAPISDHDST TO <field>. IF sy-subrc <> 0. EXIT. ENDIF. WRITE:/ 'Field Value', <field>, sy-vline. gd_str = <field> . lo_typedescr ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_DATA( <field> ). lv_fieldname = lo_typedescr->GET_RELATIVE_NAME( ). WRITE:/ 'Field Name', lv_fieldname. ENDDO. *Redo loop but convert all fields from internal to out value LOOP AT IT_BAPISDHDST INTO WA_BAPISDHDST. *Write horizonal line to screen report. WRITE:/ sy-uline. *Convert all fields to display/output versions using conversion routines PERFORM convert_all_field_values CHANGING wa_EKKO. ENDLOOP. *&---------------------------------------------------------------------* *& Form convert_all_field_values *&---------------------------------------------------------------------* FORM convert_all_field_values CHANGING p_EKKO LIKE wa_EKKO. DATA: ld_input(1000) TYPE c, ld_output(1000) TYPE C.
*Conversion exit ALPHA, internal->external for field SD_DOC CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPISDHDST-SD_DOC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPISDHDST-SD_DOC.
WRITE:/ 'New Value:', ld_input.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_BAPISDHDST_STR,
OPERATION TYPE STRING,
SD_DOC TYPE STRING,
REFDOCHDST TYPE STRING,
TOTREFSTAT TYPE STRING,
CONFIRSTAT TYPE STRING,
DELIV_STAT TYPE STRING,
DLV_STAT_H TYPE STRING,
TOTGOODSMV TYPE STRING,
BILLSTATUS TYPE STRING,
BILLINGSTA TYPE STRING,
ACCOUNTSTA TYPE STRING,
OVERALLREJ TYPE STRING,
PRC_STAT_H TYPE STRING,
OVERALLPIC TYPE STRING,
OVERALL_WM TYPE STRING,
TOTINCOMPL TYPE STRING,
TOTINCOMIT TYPE STRING,
TOTINCOMBI TYPE STRING,
GENINCOMHD TYPE STRING,
HDIMCOMDEL TYPE STRING,
HDINCOMBIL TYPE STRING,
DOCIMCOMPR TYPE STRING,
SD_DOC_CAT TYPE STRING,
SDDOCOBJ TYPE STRING,
CH_ON TYPE STRING,
BILTOTSTAT TYPE STRING,
INVOICSTAT TYPE STRING,
CUSTRESHD1 TYPE STRING,
CUSTRESHD2 TYPE STRING,
CUSTRESHD3 TYPE STRING,
CUSTRESHD4 TYPE STRING,
CUSTRESHD5 TYPE STRING,
CUSTRESIT1 TYPE STRING,
CUSTRESIT2 TYPE STRING,
CUSTRESIT3 TYPE STRING,
CUSTRESIT4 TYPE STRING,
CUSTRESIT5 TYPE STRING,
TOTSTATIT TYPE STRING,
STATSTACLC TYPE STRING,
STATDYNCLC TYPE STRING,
STATCREDCH TYPE STRING,
STATCRECH1 TYPE STRING,
STATCRECH2 TYPE STRING,
STATCRECH3 TYPE STRING,
STATCRECH4 TYPE STRING,
STATCRECH5 TYPE STRING,
STATCRECH6 TYPE STRING,
STATCRECH7 TYPE STRING,
STATCRECH8 TYPE STRING,
STATCRECH9 TYPE STRING,
STATCRECHA TYPE STRING,
STATCRECHB TYPE STRING,
STATCRECHC TYPE STRING,
TOTSTATCCH TYPE STRING,
TRNSPLANST TYPE STRING,
QUITTSTAT TYPE STRING,
CONFIRMSTA TYPE STRING,
SAPRELEASE TYPE STRING,
TOTINCOMI TYPE STRING,
TOTINCOMI1 TYPE STRING,
TOTINCOMI2 TYPE STRING,
HDINCOMPL TYPE STRING,
HDINCOMPL1 TYPE STRING,
HDINCOMPL2 TYPE STRING,
HDINCOMPL3 TYPE STRING,
CREDCHECK TYPE STRING,
DELAYSTAT TYPE STRING,
SHIPPUNIT TYPE STRING,
DISTRISTAT TYPE STRING,
REVDETSTAT TYPE STRING,
INDIDOCACH TYPE STRING,
TOBILBLOST TYPE STRING,
TODELBLOST TYPE STRING,
TOBLOCKSTA TYPE STRING,
LEBHEADSTA TYPE STRING,
FMSTATUS TYPE STRING,
SD_DOC_CAT_LONG TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPISDHDST_STR type T_EKKO_STR. DATA: ld_text TYPE string. LOOP AT IT_EKKO INTO WA_EKKO. MOVE-CORRESPONDING wa_EKKO TO WA_EKKO_STR. CONCATENATE: sy-vline
WA_BAPISDHDST_STR-OPERATION sy-vline
WA_BAPISDHDST_STR-SD_DOC sy-vline
WA_BAPISDHDST_STR-REFDOCHDST sy-vline
WA_BAPISDHDST_STR-TOTREFSTAT sy-vline
WA_BAPISDHDST_STR-CONFIRSTAT sy-vline
WA_BAPISDHDST_STR-DELIV_STAT sy-vline
WA_BAPISDHDST_STR-DLV_STAT_H sy-vline
WA_BAPISDHDST_STR-TOTGOODSMV sy-vline
WA_BAPISDHDST_STR-BILLSTATUS sy-vline
WA_BAPISDHDST_STR-BILLINGSTA sy-vline
WA_BAPISDHDST_STR-ACCOUNTSTA sy-vline
WA_BAPISDHDST_STR-OVERALLREJ sy-vline
WA_BAPISDHDST_STR-PRC_STAT_H sy-vline
WA_BAPISDHDST_STR-OVERALLPIC sy-vline
WA_BAPISDHDST_STR-OVERALL_WM sy-vline
WA_BAPISDHDST_STR-TOTINCOMPL sy-vline
WA_BAPISDHDST_STR-TOTINCOMIT sy-vline
WA_BAPISDHDST_STR-TOTINCOMBI sy-vline
WA_BAPISDHDST_STR-GENINCOMHD sy-vline
WA_BAPISDHDST_STR-HDIMCOMDEL sy-vline
WA_BAPISDHDST_STR-HDINCOMBIL sy-vline
WA_BAPISDHDST_STR-DOCIMCOMPR sy-vline
WA_BAPISDHDST_STR-SD_DOC_CAT sy-vline
WA_BAPISDHDST_STR-SDDOCOBJ sy-vline
WA_BAPISDHDST_STR-CH_ON sy-vline
WA_BAPISDHDST_STR-BILTOTSTAT sy-vline
WA_BAPISDHDST_STR-INVOICSTAT sy-vline
WA_BAPISDHDST_STR-CUSTRESHD1 sy-vline
WA_BAPISDHDST_STR-CUSTRESHD2 sy-vline
WA_BAPISDHDST_STR-CUSTRESHD3 sy-vline
WA_BAPISDHDST_STR-CUSTRESHD4 sy-vline
WA_BAPISDHDST_STR-CUSTRESHD5 sy-vline
WA_BAPISDHDST_STR-CUSTRESIT1 sy-vline
WA_BAPISDHDST_STR-CUSTRESIT2 sy-vline
WA_BAPISDHDST_STR-CUSTRESIT3 sy-vline
WA_BAPISDHDST_STR-CUSTRESIT4 sy-vline
WA_BAPISDHDST_STR-CUSTRESIT5 sy-vline
WA_BAPISDHDST_STR-TOTSTATIT sy-vline
WA_BAPISDHDST_STR-STATSTACLC sy-vline
WA_BAPISDHDST_STR-STATDYNCLC sy-vline
WA_BAPISDHDST_STR-STATCREDCH sy-vline
WA_BAPISDHDST_STR-STATCRECH1 sy-vline
WA_BAPISDHDST_STR-STATCRECH2 sy-vline
WA_BAPISDHDST_STR-STATCRECH3 sy-vline
WA_BAPISDHDST_STR-STATCRECH4 sy-vline
WA_BAPISDHDST_STR-STATCRECH5 sy-vline
WA_BAPISDHDST_STR-STATCRECH6 sy-vline
WA_BAPISDHDST_STR-STATCRECH7 sy-vline
WA_BAPISDHDST_STR-STATCRECH8 sy-vline
WA_BAPISDHDST_STR-STATCRECH9 sy-vline
WA_BAPISDHDST_STR-STATCRECHA sy-vline
WA_BAPISDHDST_STR-STATCRECHB sy-vline
WA_BAPISDHDST_STR-STATCRECHC sy-vline
WA_BAPISDHDST_STR-TOTSTATCCH sy-vline
WA_BAPISDHDST_STR-TRNSPLANST sy-vline
WA_BAPISDHDST_STR-QUITTSTAT sy-vline
WA_BAPISDHDST_STR-CONFIRMSTA sy-vline
WA_BAPISDHDST_STR-SAPRELEASE sy-vline
WA_BAPISDHDST_STR-TOTINCOMI sy-vline
WA_BAPISDHDST_STR-TOTINCOMI1 sy-vline
WA_BAPISDHDST_STR-TOTINCOMI2 sy-vline
WA_BAPISDHDST_STR-HDINCOMPL sy-vline
WA_BAPISDHDST_STR-HDINCOMPL1 sy-vline
WA_BAPISDHDST_STR-HDINCOMPL2 sy-vline
WA_BAPISDHDST_STR-HDINCOMPL3 sy-vline
WA_BAPISDHDST_STR-CREDCHECK sy-vline
WA_BAPISDHDST_STR-DELAYSTAT sy-vline
WA_BAPISDHDST_STR-SHIPPUNIT sy-vline
WA_BAPISDHDST_STR-DISTRISTAT sy-vline
WA_BAPISDHDST_STR-REVDETSTAT sy-vline
WA_BAPISDHDST_STR-INDIDOCACH sy-vline
WA_BAPISDHDST_STR-TOBILBLOST sy-vline
WA_BAPISDHDST_STR-TODELBLOST sy-vline
WA_BAPISDHDST_STR-TOBLOCKSTA sy-vline
WA_BAPISDHDST_STR-LEBHEADSTA sy-vline
WA_BAPISDHDST_STR-FMSTATUS sy-vline
WA_BAPISDHDST_STR-SD_DOC_CAT_LONG sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.