ABAP Select data from SAP table P9901 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 P9901 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 P9901. 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 P9901 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_P9901 TYPE STANDARD TABLE OF P9901,
      WA_P9901 TYPE P9901,
      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: <P9901> TYPE P9901.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P9901
*  INTO TABLE @DATA(IT_P99012).
*--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_P9901 INDEX 1 INTO DATA(WA_P99012).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P9901 ASSIGNING <P9901>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P9901>-MANDT = 1.
<P9901>-PLVAR = 1.
<P9901>-OTYPE = 1.
<P9901>-OBJID = 1.
<P9901>-INFTY = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P9901-SUBTY, sy-vline,
WA_P9901-ISTAT, sy-vline,
WA_P9901-PRIOX, sy-vline,
WA_P9901-BEGDA, sy-vline,
WA_P9901-ENDDA, sy-vline,
WA_P9901-VARYF, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P9901 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_P9901 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_P9901 INTO WA_P9901. *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 BRANCH CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_P9901-BRANCH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_P9901-BRANCH.
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_P9901_STR,
MANDT TYPE STRING,
PLVAR TYPE STRING,
OTYPE TYPE STRING,
OBJID TYPE STRING,
INFTY TYPE STRING,
SUBTY TYPE STRING,
ISTAT TYPE STRING,
PRIOX TYPE STRING,
BEGDA TYPE STRING,
ENDDA TYPE STRING,
VARYF TYPE STRING,
SEQNR TYPE STRING,
AEDTM TYPE STRING,
UNAME TYPE STRING,
REASN TYPE STRING,
HISTO TYPE STRING,
ITXNR TYPE STRING,
REQUISTION_ID TYPE STRING,
JOBTITLE TYPE STRING,
ERR_RPLY_EMAIL TYPE STRING,
COMPANY_NAME TYPE STRING,
COMPANY_DESC TYPE STRING,
COMPANY_DIV TYPE STRING,
COMPANY_DEPT TYPE STRING,
COMPANY_ADDR TYPE STRING,
COMPANY_CITY TYPE STRING,
COMPANY_STATE TYPE STRING,
COMPANY_ZIP TYPE STRING,
COMPANY_CNTRY TYPE STRING,
COMPANY_PH TYPE STRING,
COMPANY_FAX TYPE STRING,
RECRUITER_NAME TYPE STRING,
RECRUITER_EMAIL TYPE STRING,
RECRUITER_ORG TYPE STRING,
CONTACT_NAME TYPE STRING,
CONTACT_EMAIL TYPE STRING,
JOB_LOC_COUNTRY TYPE STRING,
JOB_LOC_CITY TYPE STRING,
JOB_LOC_ZIP TYPE STRING,
JOB_LOC_ACODE TYPE STRING,
JOB_LOC_STATE TYPE STRING,
CONTRACT_TYPE TYPE STRING,
WORKING_TIME TYPE STRING,
EQ_FUNC_CODE TYPE STRING,
INDUSTRY TYPE STRING,
BRANCH TYPE STRING,
COUNTRY TYPE STRING,
CITY TYPE STRING,
REGION TYPE STRING,
POSTALCODE TYPE STRING,
TRAVEL_RATIO TYPE STRING,
TEL_COM_RATIO TYPE STRING,
COMP_TYPE TYPE STRING,
COMP_AMT TYPE STRING,
COMP_MIN TYPE STRING,
COMP_MAX TYPE STRING,
CURRENCY TYPE STRING,
COMPENS_DESC TYPE STRING,
EMPL_START_DATE TYPE STRING,
EMPL_END_DATE TYPE STRING,
EDUCATION TYPE STRING,
POST_DESC TYPE STRING,
SKILL_DESC TYPE STRING,
POSTED_BOARDS TYPE STRING,
JOB_BENEFIT TYPE STRING,
EQ_POST_STATUS TYPE STRING,
CREATOR TYPE STRING,END OF T_EKKO_STR. DATA: WA_P9901_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_P9901_STR-MANDT sy-vline
WA_P9901_STR-PLVAR sy-vline
WA_P9901_STR-OTYPE sy-vline
WA_P9901_STR-OBJID sy-vline
WA_P9901_STR-INFTY sy-vline
WA_P9901_STR-SUBTY sy-vline
WA_P9901_STR-ISTAT sy-vline
WA_P9901_STR-PRIOX sy-vline
WA_P9901_STR-BEGDA sy-vline
WA_P9901_STR-ENDDA sy-vline
WA_P9901_STR-VARYF sy-vline
WA_P9901_STR-SEQNR sy-vline
WA_P9901_STR-AEDTM sy-vline
WA_P9901_STR-UNAME sy-vline
WA_P9901_STR-REASN sy-vline
WA_P9901_STR-HISTO sy-vline
WA_P9901_STR-ITXNR sy-vline
WA_P9901_STR-REQUISTION_ID sy-vline
WA_P9901_STR-JOBTITLE sy-vline
WA_P9901_STR-ERR_RPLY_EMAIL sy-vline
WA_P9901_STR-COMPANY_NAME sy-vline
WA_P9901_STR-COMPANY_DESC sy-vline
WA_P9901_STR-COMPANY_DIV sy-vline
WA_P9901_STR-COMPANY_DEPT sy-vline
WA_P9901_STR-COMPANY_ADDR sy-vline
WA_P9901_STR-COMPANY_CITY sy-vline
WA_P9901_STR-COMPANY_STATE sy-vline
WA_P9901_STR-COMPANY_ZIP sy-vline
WA_P9901_STR-COMPANY_CNTRY sy-vline
WA_P9901_STR-COMPANY_PH sy-vline
WA_P9901_STR-COMPANY_FAX sy-vline
WA_P9901_STR-RECRUITER_NAME sy-vline
WA_P9901_STR-RECRUITER_EMAIL sy-vline
WA_P9901_STR-RECRUITER_ORG sy-vline
WA_P9901_STR-CONTACT_NAME sy-vline
WA_P9901_STR-CONTACT_EMAIL sy-vline
WA_P9901_STR-JOB_LOC_COUNTRY sy-vline
WA_P9901_STR-JOB_LOC_CITY sy-vline
WA_P9901_STR-JOB_LOC_ZIP sy-vline
WA_P9901_STR-JOB_LOC_ACODE sy-vline
WA_P9901_STR-JOB_LOC_STATE sy-vline
WA_P9901_STR-CONTRACT_TYPE sy-vline
WA_P9901_STR-WORKING_TIME sy-vline
WA_P9901_STR-EQ_FUNC_CODE sy-vline
WA_P9901_STR-INDUSTRY sy-vline
WA_P9901_STR-BRANCH sy-vline
WA_P9901_STR-COUNTRY sy-vline
WA_P9901_STR-CITY sy-vline
WA_P9901_STR-REGION sy-vline
WA_P9901_STR-POSTALCODE sy-vline
WA_P9901_STR-TRAVEL_RATIO sy-vline
WA_P9901_STR-TEL_COM_RATIO sy-vline
WA_P9901_STR-COMP_TYPE sy-vline
WA_P9901_STR-COMP_AMT sy-vline
WA_P9901_STR-COMP_MIN sy-vline
WA_P9901_STR-COMP_MAX sy-vline
WA_P9901_STR-CURRENCY sy-vline
WA_P9901_STR-COMPENS_DESC sy-vline
WA_P9901_STR-EMPL_START_DATE sy-vline
WA_P9901_STR-EMPL_END_DATE sy-vline
WA_P9901_STR-EDUCATION sy-vline
WA_P9901_STR-POST_DESC sy-vline
WA_P9901_STR-SKILL_DESC sy-vline
WA_P9901_STR-POSTED_BOARDS sy-vline
WA_P9901_STR-JOB_BENEFIT sy-vline
WA_P9901_STR-EQ_POST_STATUS sy-vline
WA_P9901_STR-CREATOR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.