ABAP Select data from SAP table CARCUSTOMER 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 CARCUSTOMER 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 CARCUSTOMER. 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 CARCUSTOMER 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_CARCUSTOMER TYPE STANDARD TABLE OF CARCUSTOMER,
      WA_CARCUSTOMER TYPE CARCUSTOMER,
      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: <CARCUSTOMER> TYPE CARCUSTOMER.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM CARCUSTOMER
*  INTO TABLE @DATA(IT_CARCUSTOMER2).
*--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_CARCUSTOMER INDEX 1 INTO DATA(WA_CARCUSTOMER2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_CARCUSTOMER ASSIGNING <CARCUSTOMER>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<CARCUSTOMER>-MANDT = 1.
<CARCUSTOMER>-CUSTOMER = 1.
<CARCUSTOMER>-COMPANYCODE = 1.
<CARCUSTOMER>-SALESORGANIZATION = 1.
<CARCUSTOMER>-DISTRIBUTIONCHANNEL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_CARCUSTOMER-DIVISION, sy-vline,
WA_CARCUSTOMER-PARTNERCOUNTER, sy-vline,
WA_CARCUSTOMER-CARDNUMBER, sy-vline,
WA_CARCUSTOMER-PAYMENTCARDTYPE, sy-vline,
WA_CARCUSTOMER-PARTNERFUNCTION, sy-vline,
WA_CARCUSTOMER-CUSTOMERISWITHCOMPANYCODE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_CARCUSTOMER 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_CARCUSTOMER 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_CARCUSTOMER INTO WA_CARCUSTOMER. *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 CUSTOMER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_CARCUSTOMER-CUSTOMER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CARCUSTOMER-CUSTOMER.
WRITE:/ 'New Value:', ld_input.

*Conversion exit SXIDN, internal->external for field EMAILADDRESS CALL FUNCTION 'CONVERSION_EXIT_SXIDN_OUTPUT' EXPORTING input = WA_CARCUSTOMER-EMAILADDRESS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CARCUSTOMER-EMAILADDRESS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ISOLA, internal->external for field CORRESPONDENCELANGUAGE CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_CARCUSTOMER-CORRESPONDENCELANGUAGE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CARCUSTOMER-CORRESPONDENCELANGUAGE.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit ALPHA, internal->external for field TRADINGPARTNER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_CARCUSTOMER-TRADINGPARTNER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CARCUSTOMER-TRADINGPARTNER.
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_CARCUSTOMER_STR,
MANDT TYPE STRING,
CUSTOMER TYPE STRING,
COMPANYCODE TYPE STRING,
SALESORGANIZATION TYPE STRING,
DISTRIBUTIONCHANNEL TYPE STRING,
DIVISION TYPE STRING,
PARTNERCOUNTER TYPE STRING,
CARDNUMBER TYPE STRING,
PAYMENTCARDTYPE TYPE STRING,
PARTNERFUNCTION TYPE STRING,
CUSTOMERISWITHCOMPANYCODE TYPE STRING,
BANKIDENTIFICATION TYPE STRING,
IBAN TYPE STRING,
PARTNERFUNCTIONNAME TYPE STRING,
PERSONNELNUMBER TYPE STRING,
ACCOUNTINGCLERK TYPE STRING,
ACCOUNTINGCLERKNAME TYPE STRING,
PAYMENTTERMS TYPE STRING,
BUSINESSPARTNERNAME TYPE STRING,
CITYNAME TYPE STRING,
PHONENUMBER TYPE STRING,
EMAILADDRESS TYPE STRING,
DUNNINGPROCEDURE TYPE STRING,
DUNNINGBLOCK TYPE STRING,
CUSTOMERACCOUNTGROUP TYPE STRING,
ACCOUNTGROUPNAME TYPE STRING,
FIRSTNAME TYPE STRING,
LASTNAME TYPE STRING,
ORGANIZATIONBPNAME1 TYPE STRING,
ORGANIZATIONBPNAME2 TYPE STRING,
GROUPBUSINESSPARTNERNAME1 TYPE STRING,
GROUPBUSINESSPARTNERNAME2 TYPE STRING,
BUSINESSPARTNERTYPE TYPE STRING,
LEGALFORM TYPE STRING,
LEGALENTITYOFORGANIZATION TYPE STRING,
BUSINESSPARTNERIDBYEXTSYSTEM TYPE STRING,
ISMARKEDFORARCHIVING TYPE STRING,
BUSINESSPARTNERISBLOCKED TYPE STRING,
CONTACTPERMISSION TYPE STRING,
STREETNAME TYPE STRING,
POSTALCODE TYPE STRING,
COUNTRY TYPE STRING,
REGION TYPE STRING,
ADDRESSTIMEZONE TYPE STRING,
POBOX TYPE STRING,
POBOXPOSTALCODE TYPE STRING,
COMPANYPOSTALCODE TYPE STRING,
CORRESPONDENCELANGUAGE TYPE STRING,
MOBILEPHONENUMBER TYPE STRING,
FAXNUMBER TYPE STRING,
PRFRDCOMMMEDIUMTYPE TYPE STRING,
SEARCHTERM1 TYPE STRING,
SEARCHTERM2 TYPE STRING,
STREETPREFIXNAME TYPE STRING,
ADDITIONALSTREETPREFIXNAME TYPE STRING,
STREETSUFFIXNAME TYPE STRING,
ADDITIONALSTREETSUFFIXNAME TYPE STRING,
BUSINESSPARTNERDISTRICT TYPE STRING,
HOUSENUMBER TYPE STRING,
BUSINESSPARTNERNAME2 TYPE STRING,
BUSINESSPARTNERNAME3 TYPE STRING,
BUSINESSPARTNERNAME4 TYPE STRING,
SUPPLIER TYPE STRING,
POSTINGISBLOCKED TYPE STRING,
AUTHORIZATIONGROUP TYPE STRING,
CREATEDBYUSER TYPE STRING,
VATREGISTRATION TYPE STRING,
TAXNUMBER1 TYPE STRING,
TAXNUMBER2 TYPE STRING,
TAXNUMBER3 TYPE STRING,
TAXNUMBER4 TYPE STRING,
TAXNUMBER5 TYPE STRING,
VATLIABILITY TYPE STRING,
RESPONSIBLETYPE TYPE STRING,
TAXNUMBERTYPE TYPE STRING,
SALESOFFICE TYPE STRING,
SALESGROUP TYPE STRING,
CUSTOMERPAYMENTTERMS TYPE STRING,
LASTDUNNEDON TYPE STRING,
LEGDUNNINGPROCEDUREON TYPE STRING,
DUNNINGLEVEL TYPE STRING,
PHYSICALINVENTORYBLOCKIND TYPE STRING,
CREDITMEMOPAYMENTTERMS TYPE STRING,
PAYMENTMETHODSLIST TYPE STRING,
HOUSEBANK TYPE STRING,
PAYMENTBLOCKINGREASON TYPE STRING,
DUNNINGNOTICEGROUP TYPE STRING,
ACCOUNTBYCUSTOMER TYPE STRING,
USERATCUSTOMER TYPE STRING,
ACCOUNTINGCLERKPHONENUMBER TYPE STRING,
ACCOUNTINGCLERKINTERNETADDRESS TYPE STRING,
RECONCILIATIONACCOUNT TYPE STRING,
INTERESTCALCULATIONCODE TYPE STRING,
DUNNINGRECIPIENT TYPE STRING,
DUNNINGCLERK TYPE STRING,
INTRSTCALCFREQUENCYINMONTHS TYPE STRING,
INTERESTCALCULATIONDATE TYPE STRING,
LASTINTERESTCALCRUNDATE TYPE STRING,
BPCUSTOMERNUMBER TYPE STRING,
BUSINESSPARTNERISVIP TYPE STRING,
CUSTOMERISUNWANTED TYPE STRING,
UNDESIRABILITYREASON TYPE STRING,
UNDESIRABILITYCOMMENT TYPE STRING,
TRADINGPARTNER TYPE STRING,
LASTCUSTOMERCONTACTDATE TYPE STRING,
STATUS TYPE STRING,
SYSTEMSTATUSSHORTNAME TYPE STRING,
ADDRESSTEXT TYPE STRING,
CUSTOMERLISTKEYFIELD1 TYPE STRING,
CUSTOMERLISTKEYFIELD2 TYPE STRING,
CUSTOMERLISTKEYFIELD3 TYPE STRING,
CUSTOMERLISTKEYFIELD4 TYPE STRING,
BRANCHCODE TYPE STRING,
TH_BRANCHCODEDESCRIPTION TYPE STRING,
FULLNAME TYPE STRING,END OF T_EKKO_STR. DATA: WA_CARCUSTOMER_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_CARCUSTOMER_STR-MANDT sy-vline
WA_CARCUSTOMER_STR-CUSTOMER sy-vline
WA_CARCUSTOMER_STR-COMPANYCODE sy-vline
WA_CARCUSTOMER_STR-SALESORGANIZATION sy-vline
WA_CARCUSTOMER_STR-DISTRIBUTIONCHANNEL sy-vline
WA_CARCUSTOMER_STR-DIVISION sy-vline
WA_CARCUSTOMER_STR-PARTNERCOUNTER sy-vline
WA_CARCUSTOMER_STR-CARDNUMBER sy-vline
WA_CARCUSTOMER_STR-PAYMENTCARDTYPE sy-vline
WA_CARCUSTOMER_STR-PARTNERFUNCTION sy-vline
WA_CARCUSTOMER_STR-CUSTOMERISWITHCOMPANYCODE sy-vline
WA_CARCUSTOMER_STR-BANKIDENTIFICATION sy-vline
WA_CARCUSTOMER_STR-IBAN sy-vline
WA_CARCUSTOMER_STR-PARTNERFUNCTIONNAME sy-vline
WA_CARCUSTOMER_STR-PERSONNELNUMBER sy-vline
WA_CARCUSTOMER_STR-ACCOUNTINGCLERK sy-vline
WA_CARCUSTOMER_STR-ACCOUNTINGCLERKNAME sy-vline
WA_CARCUSTOMER_STR-PAYMENTTERMS sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERNAME sy-vline
WA_CARCUSTOMER_STR-CITYNAME sy-vline
WA_CARCUSTOMER_STR-PHONENUMBER sy-vline
WA_CARCUSTOMER_STR-EMAILADDRESS sy-vline
WA_CARCUSTOMER_STR-DUNNINGPROCEDURE sy-vline
WA_CARCUSTOMER_STR-DUNNINGBLOCK sy-vline
WA_CARCUSTOMER_STR-CUSTOMERACCOUNTGROUP sy-vline
WA_CARCUSTOMER_STR-ACCOUNTGROUPNAME sy-vline
WA_CARCUSTOMER_STR-FIRSTNAME sy-vline
WA_CARCUSTOMER_STR-LASTNAME sy-vline
WA_CARCUSTOMER_STR-ORGANIZATIONBPNAME1 sy-vline
WA_CARCUSTOMER_STR-ORGANIZATIONBPNAME2 sy-vline
WA_CARCUSTOMER_STR-GROUPBUSINESSPARTNERNAME1 sy-vline
WA_CARCUSTOMER_STR-GROUPBUSINESSPARTNERNAME2 sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERTYPE sy-vline
WA_CARCUSTOMER_STR-LEGALFORM sy-vline
WA_CARCUSTOMER_STR-LEGALENTITYOFORGANIZATION sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERIDBYEXTSYSTEM sy-vline
WA_CARCUSTOMER_STR-ISMARKEDFORARCHIVING sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERISBLOCKED sy-vline
WA_CARCUSTOMER_STR-CONTACTPERMISSION sy-vline
WA_CARCUSTOMER_STR-STREETNAME sy-vline
WA_CARCUSTOMER_STR-POSTALCODE sy-vline
WA_CARCUSTOMER_STR-COUNTRY sy-vline
WA_CARCUSTOMER_STR-REGION sy-vline
WA_CARCUSTOMER_STR-ADDRESSTIMEZONE sy-vline
WA_CARCUSTOMER_STR-POBOX sy-vline
WA_CARCUSTOMER_STR-POBOXPOSTALCODE sy-vline
WA_CARCUSTOMER_STR-COMPANYPOSTALCODE sy-vline
WA_CARCUSTOMER_STR-CORRESPONDENCELANGUAGE sy-vline
WA_CARCUSTOMER_STR-MOBILEPHONENUMBER sy-vline
WA_CARCUSTOMER_STR-FAXNUMBER sy-vline
WA_CARCUSTOMER_STR-PRFRDCOMMMEDIUMTYPE sy-vline
WA_CARCUSTOMER_STR-SEARCHTERM1 sy-vline
WA_CARCUSTOMER_STR-SEARCHTERM2 sy-vline
WA_CARCUSTOMER_STR-STREETPREFIXNAME sy-vline
WA_CARCUSTOMER_STR-ADDITIONALSTREETPREFIXNAME sy-vline
WA_CARCUSTOMER_STR-STREETSUFFIXNAME sy-vline
WA_CARCUSTOMER_STR-ADDITIONALSTREETSUFFIXNAME sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERDISTRICT sy-vline
WA_CARCUSTOMER_STR-HOUSENUMBER sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERNAME2 sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERNAME3 sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERNAME4 sy-vline
WA_CARCUSTOMER_STR-SUPPLIER sy-vline
WA_CARCUSTOMER_STR-POSTINGISBLOCKED sy-vline
WA_CARCUSTOMER_STR-AUTHORIZATIONGROUP sy-vline
WA_CARCUSTOMER_STR-CREATEDBYUSER sy-vline
WA_CARCUSTOMER_STR-VATREGISTRATION sy-vline
WA_CARCUSTOMER_STR-TAXNUMBER1 sy-vline
WA_CARCUSTOMER_STR-TAXNUMBER2 sy-vline
WA_CARCUSTOMER_STR-TAXNUMBER3 sy-vline
WA_CARCUSTOMER_STR-TAXNUMBER4 sy-vline
WA_CARCUSTOMER_STR-TAXNUMBER5 sy-vline
WA_CARCUSTOMER_STR-VATLIABILITY sy-vline
WA_CARCUSTOMER_STR-RESPONSIBLETYPE sy-vline
WA_CARCUSTOMER_STR-TAXNUMBERTYPE sy-vline
WA_CARCUSTOMER_STR-SALESOFFICE sy-vline
WA_CARCUSTOMER_STR-SALESGROUP sy-vline
WA_CARCUSTOMER_STR-CUSTOMERPAYMENTTERMS sy-vline
WA_CARCUSTOMER_STR-LASTDUNNEDON sy-vline
WA_CARCUSTOMER_STR-LEGDUNNINGPROCEDUREON sy-vline
WA_CARCUSTOMER_STR-DUNNINGLEVEL sy-vline
WA_CARCUSTOMER_STR-PHYSICALINVENTORYBLOCKIND sy-vline
WA_CARCUSTOMER_STR-CREDITMEMOPAYMENTTERMS sy-vline
WA_CARCUSTOMER_STR-PAYMENTMETHODSLIST sy-vline
WA_CARCUSTOMER_STR-HOUSEBANK sy-vline
WA_CARCUSTOMER_STR-PAYMENTBLOCKINGREASON sy-vline
WA_CARCUSTOMER_STR-DUNNINGNOTICEGROUP sy-vline
WA_CARCUSTOMER_STR-ACCOUNTBYCUSTOMER sy-vline
WA_CARCUSTOMER_STR-USERATCUSTOMER sy-vline
WA_CARCUSTOMER_STR-ACCOUNTINGCLERKPHONENUMBER sy-vline
WA_CARCUSTOMER_STR-ACCOUNTINGCLERKINTERNETADDRESS sy-vline
WA_CARCUSTOMER_STR-RECONCILIATIONACCOUNT sy-vline
WA_CARCUSTOMER_STR-INTERESTCALCULATIONCODE sy-vline
WA_CARCUSTOMER_STR-DUNNINGRECIPIENT sy-vline
WA_CARCUSTOMER_STR-DUNNINGCLERK sy-vline
WA_CARCUSTOMER_STR-INTRSTCALCFREQUENCYINMONTHS sy-vline
WA_CARCUSTOMER_STR-INTERESTCALCULATIONDATE sy-vline
WA_CARCUSTOMER_STR-LASTINTERESTCALCRUNDATE sy-vline
WA_CARCUSTOMER_STR-BPCUSTOMERNUMBER sy-vline
WA_CARCUSTOMER_STR-BUSINESSPARTNERISVIP sy-vline
WA_CARCUSTOMER_STR-CUSTOMERISUNWANTED sy-vline
WA_CARCUSTOMER_STR-UNDESIRABILITYREASON sy-vline
WA_CARCUSTOMER_STR-UNDESIRABILITYCOMMENT sy-vline
WA_CARCUSTOMER_STR-TRADINGPARTNER sy-vline
WA_CARCUSTOMER_STR-LASTCUSTOMERCONTACTDATE sy-vline
WA_CARCUSTOMER_STR-STATUS sy-vline
WA_CARCUSTOMER_STR-SYSTEMSTATUSSHORTNAME sy-vline
WA_CARCUSTOMER_STR-ADDRESSTEXT sy-vline
WA_CARCUSTOMER_STR-CUSTOMERLISTKEYFIELD1 sy-vline
WA_CARCUSTOMER_STR-CUSTOMERLISTKEYFIELD2 sy-vline
WA_CARCUSTOMER_STR-CUSTOMERLISTKEYFIELD3 sy-vline
WA_CARCUSTOMER_STR-CUSTOMERLISTKEYFIELD4 sy-vline
WA_CARCUSTOMER_STR-BRANCHCODE sy-vline
WA_CARCUSTOMER_STR-TH_BRANCHCODEDESCRIPTION sy-vline
WA_CARCUSTOMER_STR-FULLNAME sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.