ABAP Select data from SAP table BAPI_CTRACPSOBJECT_ACCOUNT 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 BAPI_CTRACPSOBJECT_ACCOUNT 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 BAPI_CTRACPSOBJECT_ACCOUNT. 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 BAPI_CTRACPSOBJECT_ACCOUNT 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_BAPI_CTRACPSOBJECT_ACCOUNT TYPE STANDARD TABLE OF BAPI_CTRACPSOBJECT_ACCOUNT,
      WA_BAPI_CTRACPSOBJECT_ACCOUNT TYPE BAPI_CTRACPSOBJECT_ACCOUNT,
      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: <BAPI_CTRACPSOBJECT_ACCOUNT> TYPE BAPI_CTRACPSOBJECT_ACCOUNT.

*Process all fields in table header/work area as string values
  PERFORM process_as_string_field_values CHANGING wa_BAPI_CTRACPSOBJECT_ACCOUNT.

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM BAPI_CTRACPSOBJECT_ACCOUNT
  INTO TABLE IT_BAPI_CTRACPSOBJECT_ACCOUNT.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPI_CTRACPSOBJECT_ACCOUNT
*  INTO TABLE @DATA(IT_BAPI_CTRACPSOBJECT_ACCOUNT2).
*--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_BAPI_CTRACPSOBJECT_ACCOUNT INDEX 1 INTO DATA(WA_BAPI_CTRACPSOBJECT_ACCOUNT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPI_CTRACPSOBJECT_ACCOUNT ASSIGNING <BAPI_CTRACPSOBJECT_ACCOUNT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPI_CTRACPSOBJECT_ACCOUNT>-PSOBJECTKEY = 1.
<BAPI_CTRACPSOBJECT_ACCOUNT>-PARTNER = 1.
<BAPI_CTRACPSOBJECT_ACCOUNT>-CTRACCOUNTTYPE = 1.
<BAPI_CTRACPSOBJECT_ACCOUNT>-CTRACCOUNT = 1.
<BAPI_CTRACPSOBJECT_ACCOUNT>-PSLEGACYOBJECT = 1.
ENDLOOP.

LOOP AT IT_BAPI_CTRACPSOBJECT_ACCOUNT INTO WA_BAPI_CTRACPSOBJECT_ACCOUNT.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPI_CTRACPSOBJECT_ACCOUNT-CREATED_BY, sy-vline,
WA_BAPI_CTRACPSOBJECT_ACCOUNT-CREATED_ON, sy-vline,
WA_BAPI_CTRACPSOBJECT_ACCOUNT-CHANGED_BY, sy-vline,
WA_BAPI_CTRACPSOBJECT_ACCOUNT-CHANGED_ON, sy-vline,
WA_BAPI_CTRACPSOBJECT_ACCOUNT-PSOBJECT_PAY_ACT, sy-vline,
WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_NO, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPI_CTRACPSOBJECT_ACCOUNT 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_BAPI_CTRACPSOBJECT_ACCOUNT 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_BAPI_CTRACPSOBJECT_ACCOUNT INTO WA_BAPI_CTRACPSOBJECT_ACCOUNT. *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 PSOBJECTKEY CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-PSOBJECTKEY IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-PSOBJECTKEY.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PARTNER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-PARTNER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-PARTNER.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field CTRACCOUNT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-CTRACCOUNT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-CTRACCOUNT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADDR_NO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_NO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_NO.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ALTER_PAYER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALTER_PAYER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALTER_PAYER.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADDR_ALT_PAYER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_PAYER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_PAYER.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ALTER_PAYEE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALTER_PAYEE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALTER_PAYEE.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADDR_ALT_PAYEE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_PAYEE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_PAYEE.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ALT_CORR_RECIPIENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALT_CORR_RECIPIENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ALT_CORR_RECIPIENT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field CORR_FIRST_YEAR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-CORR_FIRST_YEAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-CORR_FIRST_YEAR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field CORR_LAST_YEAR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-CORR_LAST_YEAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-CORR_LAST_YEAR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field COLLPERSON CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-COLLPERSON IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-COLLPERSON.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADDR_ALT_CORR_RECIPIENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_CORR_RECIPIENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CTRACPSOBJECT_ACCOUNT-ADDR_ALT_CORR_RECIPIENT.
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_BAPI_CTRACPSOBJECT_ACCOUNT_STR,
PSOBJECTKEY TYPE STRING,
PARTNER TYPE STRING,
CTRACCOUNTTYPE TYPE STRING,
CTRACCOUNT TYPE STRING,
PSLEGACYOBJECT TYPE STRING,
CREATED_BY TYPE STRING,
CREATED_ON TYPE STRING,
CHANGED_BY TYPE STRING,
CHANGED_ON TYPE STRING,
PSOBJECT_PAY_ACT TYPE STRING,
ADDR_NO TYPE STRING,
ALTER_PAYER TYPE STRING,
ADDR_ALT_PAYER TYPE STRING,
METHOD_INC TYPE STRING,
BANK_INC TYPE STRING,
CCARD_INC TYPE STRING,
ALTER_PAYEE TYPE STRING,
ADDR_ALT_PAYEE TYPE STRING,
METHOD_OUTG TYPE STRING,
BANK_OUTG TYPE STRING,
CCARD_OUTG TYPE STRING,
PSOBJECT_CORR_ACT TYPE STRING,
ALT_CORR_RECIPIENT TYPE STRING,
CORR_ACTIVITY_KEY TYPE STRING,
CORR_VARIANT TYPE STRING,
SEPERATE_INVOICE TYPE STRING,
INVOICE_TYPE TYPE STRING,
CORR_STATUS TYPE STRING,
CORR_PERIOD TYPE STRING,
CORR_OFFSET_DAYS TYPE STRING,
CORR_OFFSET_MONTHS TYPE STRING,
CORR_DUNN_VARIANT TYPE STRING,
CORR_FIRST_PERIOD TYPE STRING,
CORR_FIRST_YEAR TYPE STRING,
CORR_LAST_PERIOD TYPE STRING,
CORR_LAST_YEAR TYPE STRING,
OBSOLETEFLAG TYPE STRING,
ACCOUNT_DETERMINATION_ID TYPE STRING,
OWN_BANK_ACCOUNT TYPE STRING,
COLLGROUP TYPE STRING,
COLLSTRAT TYPE STRING,
DIRECT_DEBIT_AMOUNT TYPE STRING,
DIRECT_DEBIT_CURRENCY TYPE STRING,
DIRECT_DEBIT_CURRENCY_ISO TYPE STRING,
DIRECT_DEBIT_MONTHS TYPE STRING,
DIRECT_DEBIT_ROLLING TYPE STRING,
COLLPERSON TYPE STRING,
CORR_INDIVIDUAL TYPE STRING,
XREMINDER TYPE STRING,
SEPA_MANDATE_ID TYPE STRING,
ARCHIVINGFLAG TYPE STRING,
ADDR_ALT_CORR_RECIPIENT TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPI_CTRACPSOBJECT_ACCOUNT_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_BAPI_CTRACPSOBJECT_ACCOUNT_STR-PSOBJECTKEY sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-PARTNER sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CTRACCOUNTTYPE sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CTRACCOUNT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-PSLEGACYOBJECT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CREATED_BY sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CREATED_ON sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CHANGED_BY sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CHANGED_ON sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-PSOBJECT_PAY_ACT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ADDR_NO sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ALTER_PAYER sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ADDR_ALT_PAYER sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-METHOD_INC sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-BANK_INC sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CCARD_INC sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ALTER_PAYEE sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ADDR_ALT_PAYEE sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-METHOD_OUTG sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-BANK_OUTG sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CCARD_OUTG sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-PSOBJECT_CORR_ACT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ALT_CORR_RECIPIENT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_ACTIVITY_KEY sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_VARIANT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-SEPERATE_INVOICE sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-INVOICE_TYPE sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_STATUS sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_PERIOD sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_OFFSET_DAYS sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_OFFSET_MONTHS sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_DUNN_VARIANT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_FIRST_PERIOD sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_FIRST_YEAR sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_LAST_PERIOD sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_LAST_YEAR sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-OBSOLETEFLAG sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ACCOUNT_DETERMINATION_ID sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-OWN_BANK_ACCOUNT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-COLLGROUP sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-COLLSTRAT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-DIRECT_DEBIT_AMOUNT sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-DIRECT_DEBIT_CURRENCY sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-DIRECT_DEBIT_CURRENCY_ISO sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-DIRECT_DEBIT_MONTHS sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-DIRECT_DEBIT_ROLLING sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-COLLPERSON sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-CORR_INDIVIDUAL sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-XREMINDER sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-SEPA_MANDATE_ID sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ARCHIVINGFLAG sy-vline
WA_BAPI_CTRACPSOBJECT_ACCOUNT_STR-ADDR_ALT_CORR_RECIPIENT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.