ABAP Select data from SAP table IINJRYILLNCASE 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 IINJRYILLNCASE 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 IINJRYILLNCASE. 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 IINJRYILLNCASE 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_IINJRYILLNCASE TYPE STANDARD TABLE OF IINJRYILLNCASE,
      WA_IINJRYILLNCASE TYPE IINJRYILLNCASE,
      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: <IINJRYILLNCASE> TYPE IINJRYILLNCASE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM IINJRYILLNCASE
*  INTO TABLE @DATA(IT_IINJRYILLNCASE2).
*--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_IINJRYILLNCASE INDEX 1 INTO DATA(WA_IINJRYILLNCASE2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_IINJRYILLNCASE ASSIGNING <IINJRYILLNCASE>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<IINJRYILLNCASE>-MANDT = 1.
<IINJRYILLNCASE>-INJUREDPERSONINFOUUID = 1.
<IINJRYILLNCASE>-INCIDENTUUID = 1.
<IINJRYILLNCASE>-INVOLVEDPERSONUUID = 1.
<IINJRYILLNCASE>-INCIDENTID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_IINJRYILLNCASE-INCIDENTSTATUS, sy-vline,
WA_IINJRYILLNCASE-INCIDENTCATEGORY, sy-vline,
WA_IINJRYILLNCASE-INCIDENTMAJORROOTCAUSE, sy-vline,
WA_IINJRYILLNCASE-INCIDENTINVESTIGATIONSTATUS, sy-vline,
WA_IINJRYILLNCASE-INCIDENTTITLE, sy-vline,
WA_IINJRYILLNCASE-COUNTRY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_IINJRYILLNCASE 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_IINJRYILLNCASE 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_IINJRYILLNCASE INTO WA_IINJRYILLNCASE. *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 INCIDENTID CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IINJRYILLNCASE-INCIDENTID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IINJRYILLNCASE-INCIDENTID.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field EHSLOCATIONID CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IINJRYILLNCASE-EHSLOCATIONID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IINJRYILLNCASE-EHSLOCATIONID.
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_IINJRYILLNCASE_STR,
MANDT TYPE STRING,
INJUREDPERSONINFOUUID TYPE STRING,
INCIDENTUUID TYPE STRING,
INVOLVEDPERSONUUID TYPE STRING,
INCIDENTID TYPE STRING,
INCIDENTSTATUS TYPE STRING,
INCIDENTCATEGORY TYPE STRING,
INCIDENTMAJORROOTCAUSE TYPE STRING,
INCIDENTINVESTIGATIONSTATUS TYPE STRING,
INCIDENTTITLE TYPE STRING,
COUNTRY TYPE STRING,
REGION TYPE STRING,
PLANT TYPE STRING,
ORGANIZATIONALUNIT TYPE STRING,
EHSLOCATIONUUID TYPE STRING,
EHSLOCATIONID TYPE STRING,
INCIDENTDATE TYPE STRING,
INCIDENTMONTH TYPE STRING,
INCIDENTYEAR TYPE STRING,
INCIDENTTIMEISUNKNOWN TYPE STRING,
INCIDENTTIME TYPE STRING,
INCIDENTHOUR TYPE STRING,
INCIDENTWEEKDAY TYPE STRING,
INCIDENTHASACCESSRESTRICTION TYPE STRING,
PERSONINFOHASACCRESTRICTION TYPE STRING,
INJURYILLNESSCLASSIFICATION TYPE STRING,
MAININJURYILLNESSTYPE TYPE STRING,
MAININJURYILLNESSBODYPART TYPE STRING,
MAININJURYILLNESSBODYSIDE TYPE STRING,
INJURYILLNESSCASEISRECORDABLE TYPE STRING,
INCIDENTINJURYILLNLOGTYPE TYPE STRING,
NUMBEROFINJURYILLNESSCASES TYPE STRING,
INJRYILLNCASESVRTYCLASSFCTN TYPE STRING,
NUMBEROFDEATHCASES TYPE STRING,
NMBROFCASESWTHDAYSAWAYFRMWORK TYPE STRING,
NMBROFCASESWITHTRANSFORRSTRCN TYPE STRING,
NUMBEROFOTHERINJRYILLNESSCASES TYPE STRING,
MAININJURYILLNESSCLASSFCTN TYPE STRING,
NUMBEROFINJURYCASES TYPE STRING,
NUMBEROFSKINDISORDERCASES TYPE STRING,
NMBROFRESPIRATORYCNDNCASES TYPE STRING,
NUMBEROFPOISONINGCASES TYPE STRING,
NUMBEROFHEARINGLOSSCASES TYPE STRING,
NUMBEROFOTHERILLNESSCASES TYPE STRING,
NMBROFILLNESSCASESNOTASSIGNED TYPE STRING,
NMBROFCALENDARDAYSAWAYFROMWORK TYPE STRING,
NMBROFRSTRCDANDTRANSFCALDAYS TYPE STRING,
NUMBEROFAFFECTEDCALDAYS TYPE STRING,END OF T_EKKO_STR. DATA: WA_IINJRYILLNCASE_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_IINJRYILLNCASE_STR-MANDT sy-vline
WA_IINJRYILLNCASE_STR-INJUREDPERSONINFOUUID sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTUUID sy-vline
WA_IINJRYILLNCASE_STR-INVOLVEDPERSONUUID sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTID sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTSTATUS sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTCATEGORY sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTMAJORROOTCAUSE sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTINVESTIGATIONSTATUS sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTTITLE sy-vline
WA_IINJRYILLNCASE_STR-COUNTRY sy-vline
WA_IINJRYILLNCASE_STR-REGION sy-vline
WA_IINJRYILLNCASE_STR-PLANT sy-vline
WA_IINJRYILLNCASE_STR-ORGANIZATIONALUNIT sy-vline
WA_IINJRYILLNCASE_STR-EHSLOCATIONUUID sy-vline
WA_IINJRYILLNCASE_STR-EHSLOCATIONID sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTDATE sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTMONTH sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTYEAR sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTTIMEISUNKNOWN sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTTIME sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTHOUR sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTWEEKDAY sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTHASACCESSRESTRICTION sy-vline
WA_IINJRYILLNCASE_STR-PERSONINFOHASACCRESTRICTION sy-vline
WA_IINJRYILLNCASE_STR-INJURYILLNESSCLASSIFICATION sy-vline
WA_IINJRYILLNCASE_STR-MAININJURYILLNESSTYPE sy-vline
WA_IINJRYILLNCASE_STR-MAININJURYILLNESSBODYPART sy-vline
WA_IINJRYILLNCASE_STR-MAININJURYILLNESSBODYSIDE sy-vline
WA_IINJRYILLNCASE_STR-INJURYILLNESSCASEISRECORDABLE sy-vline
WA_IINJRYILLNCASE_STR-INCIDENTINJURYILLNLOGTYPE sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFINJURYILLNESSCASES sy-vline
WA_IINJRYILLNCASE_STR-INJRYILLNCASESVRTYCLASSFCTN sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFDEATHCASES sy-vline
WA_IINJRYILLNCASE_STR-NMBROFCASESWTHDAYSAWAYFRMWORK sy-vline
WA_IINJRYILLNCASE_STR-NMBROFCASESWITHTRANSFORRSTRCN sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFOTHERINJRYILLNESSCASES sy-vline
WA_IINJRYILLNCASE_STR-MAININJURYILLNESSCLASSFCTN sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFINJURYCASES sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFSKINDISORDERCASES sy-vline
WA_IINJRYILLNCASE_STR-NMBROFRESPIRATORYCNDNCASES sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFPOISONINGCASES sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFHEARINGLOSSCASES sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFOTHERILLNESSCASES sy-vline
WA_IINJRYILLNCASE_STR-NMBROFILLNESSCASESNOTASSIGNED sy-vline
WA_IINJRYILLNCASE_STR-NMBROFCALENDARDAYSAWAYFROMWORK sy-vline
WA_IINJRYILLNCASE_STR-NMBROFRSTRCDANDTRANSFCALDAYS sy-vline
WA_IINJRYILLNCASE_STR-NUMBEROFAFFECTEDCALDAYS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.