ABAP Select data from SAP table BAPI2097MOVE 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 BAPI2097MOVE 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 BAPI2097MOVE. 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 BAPI2097MOVE 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_BAPI2097MOVE TYPE STANDARD TABLE OF BAPI2097MOVE,
      WA_BAPI2097MOVE TYPE BAPI2097MOVE,
      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: <BAPI2097MOVE> TYPE BAPI2097MOVE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPI2097MOVE
*  INTO TABLE @DATA(IT_BAPI2097MOVE2).
*--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_BAPI2097MOVE INDEX 1 INTO DATA(WA_BAPI2097MOVE2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPI2097MOVE ASSIGNING <BAPI2097MOVE>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPI2097MOVE>-CLIENT = 1.
<BAPI2097MOVE>-INSTITUTION = 1.
<BAPI2097MOVE>-PATCASEID = 1.
<BAPI2097MOVE>-MOVEMNT_SEQNO = 1.
<BAPI2097MOVE>-MOVEMNT_CTGRY = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPI2097MOVE-MOVEMNT_TYPE, sy-vline,
WA_BAPI2097MOVE-MOVEMNT_DATE, sy-vline,
WA_BAPI2097MOVE-MOVEMNT_TIME, sy-vline,
WA_BAPI2097MOVE-STATUS_IND, sy-vline,
WA_BAPI2097MOVE-INT_VISIT_STAT, sy-vline,
WA_BAPI2097MOVE-MOVEMNT_PDATE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPI2097MOVE 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_BAPI2097MOVE 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_BAPI2097MOVE INTO WA_BAPI2097MOVE. *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 INSTITUTION CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI2097MOVE-INSTITUTION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI2097MOVE-INSTITUTION.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field TREAT_CODE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI2097MOVE-TREAT_CODE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI2097MOVE-TREAT_CODE.
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_BAPI2097MOVE_STR,
CLIENT TYPE STRING,
INSTITUTION TYPE STRING,
PATCASEID TYPE STRING,
MOVEMNT_SEQNO TYPE STRING,
MOVEMNT_CTGRY TYPE STRING,
MOVEMNT_TYPE TYPE STRING,
MOVEMNT_DATE TYPE STRING,
MOVEMNT_TIME TYPE STRING,
STATUS_IND TYPE STRING,
INT_VISIT_STAT TYPE STRING,
MOVEMNT_PDATE TYPE STRING,
MOVEMNT_PTIME TYPE STRING,
MOVEMNT_ENDDATE TYPE STRING,
MOVEMNT_ENDTIME TYPE STRING,
STATUS_IND_END TYPE STRING,
MOVEMNT_COMMENT TYPE STRING,
MOVEMNT_LTEXT TYPE STRING,
MOVEMNT_REAS1 TYPE STRING,
MOVEMNT_REAS2 TYPE STRING,
REFMOVE_SEQNO TYPE STRING,
PHONENO TYPE STRING,
TV TYPE STRING,
EMERG_CASE TYPE STRING,
ACCIDENT TYPE STRING,
ACCIDENT_NO TYPE STRING,
ACCIDENT_LOC TYPE STRING,
ACCIDENT_TIME TYPE STRING,
ACCIDENT_DATE TYPE STRING,
ACCIDENT_CMMNT TYPE STRING,
ACCIDENT_LTEXT TYPE STRING,
ACCIDENT_EMSTYP TYPE STRING,
LATEST_ADM TYPE STRING,
DEPARTMENT TYPE STRING,
NURS_TREAT_OU TYPE STRING,
ROOM TYPE STRING,
BED TYPE STRING,
ROOM_STATUS_IND TYPE STRING,
LENGTH_OF_STAY TYPE STRING,
REF_HOSPITAL TYPE STRING,
ADMITT_DEPT TYPE STRING,
DISCHRG_DISP TYPE STRING,
C_OF_DEATH TYPE STRING,
WORK_INCAPACITY TYPE STRING,
ABSNCE_APPROV TYPE STRING,
ACCIDENT_EMSVCE TYPE STRING,
TREATMCATEGORY TYPE STRING,
SUBSEQ_MOVE TYPE STRING,
PRIOR_MOVE TYPE STRING,
NO_OF_CALLS TYPE STRING,
MEDICAL_TEXT TYPE STRING,
MEDICAL_LTEXT TYPE STRING,
PRIORITY TYPE STRING,
SCHEDUL_TYPE TYPE STRING,
VISITCATEGORY TYPE STRING,
TRANSPORT_TYPE TYPE STRING,
ACCIDENT_3RDPTY TYPE STRING,
GEN_EVENT TYPE STRING,
SURGERY_TYPE TYPE STRING,
CLASS TYPE STRING,
REF_PSTTRT_TYPE TYPE STRING,
SPECIALTY TYPE STRING,
ARRIVAL_MODE TYPE STRING,
WAITLIST_PRIO TYPE STRING,
WAITLIST_TYPE TYPE STRING,
WAITLIST_DELDAT TYPE STRING,
WAITLIST_DELREAS TYPE STRING,
WAITLIST_STATUS TYPE STRING,
WAITLIST_HOSP TYPE STRING,
WAITLIST_INCLON TYPE STRING,
CREATION_DATE TYPE STRING,
CREATION_USER TYPE STRING,
UPDATE_DATE TYPE STRING,
UPDATE_USER TYPE STRING,
CANCEL_IND TYPE STRING,
CANCEL_USER TYPE STRING,
CANCEL_DATE TYPE STRING,
CANCEL_REASON TYPE STRING,
TREAT_CODE TYPE STRING,
CREATION_TIME TYPE STRING,
PRIORITY_LONG TYPE STRING,
EXT_MOVEMENT_ID TYPE STRING,
SAPS TYPE STRING,
REF_HOSPITAL_NAME2 TYPE STRING,
REF_HOSPITAL_NAME3 TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPI2097MOVE_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_BAPI2097MOVE_STR-CLIENT sy-vline
WA_BAPI2097MOVE_STR-INSTITUTION sy-vline
WA_BAPI2097MOVE_STR-PATCASEID sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_SEQNO sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_CTGRY sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_TYPE sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_DATE sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_TIME sy-vline
WA_BAPI2097MOVE_STR-STATUS_IND sy-vline
WA_BAPI2097MOVE_STR-INT_VISIT_STAT sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_PDATE sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_PTIME sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_ENDDATE sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_ENDTIME sy-vline
WA_BAPI2097MOVE_STR-STATUS_IND_END sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_COMMENT sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_LTEXT sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_REAS1 sy-vline
WA_BAPI2097MOVE_STR-MOVEMNT_REAS2 sy-vline
WA_BAPI2097MOVE_STR-REFMOVE_SEQNO sy-vline
WA_BAPI2097MOVE_STR-PHONENO sy-vline
WA_BAPI2097MOVE_STR-TV sy-vline
WA_BAPI2097MOVE_STR-EMERG_CASE sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_NO sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_LOC sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_TIME sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_DATE sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_CMMNT sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_LTEXT sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_EMSTYP sy-vline
WA_BAPI2097MOVE_STR-LATEST_ADM sy-vline
WA_BAPI2097MOVE_STR-DEPARTMENT sy-vline
WA_BAPI2097MOVE_STR-NURS_TREAT_OU sy-vline
WA_BAPI2097MOVE_STR-ROOM sy-vline
WA_BAPI2097MOVE_STR-BED sy-vline
WA_BAPI2097MOVE_STR-ROOM_STATUS_IND sy-vline
WA_BAPI2097MOVE_STR-LENGTH_OF_STAY sy-vline
WA_BAPI2097MOVE_STR-REF_HOSPITAL sy-vline
WA_BAPI2097MOVE_STR-ADMITT_DEPT sy-vline
WA_BAPI2097MOVE_STR-DISCHRG_DISP sy-vline
WA_BAPI2097MOVE_STR-C_OF_DEATH sy-vline
WA_BAPI2097MOVE_STR-WORK_INCAPACITY sy-vline
WA_BAPI2097MOVE_STR-ABSNCE_APPROV sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_EMSVCE sy-vline
WA_BAPI2097MOVE_STR-TREATMCATEGORY sy-vline
WA_BAPI2097MOVE_STR-SUBSEQ_MOVE sy-vline
WA_BAPI2097MOVE_STR-PRIOR_MOVE sy-vline
WA_BAPI2097MOVE_STR-NO_OF_CALLS sy-vline
WA_BAPI2097MOVE_STR-MEDICAL_TEXT sy-vline
WA_BAPI2097MOVE_STR-MEDICAL_LTEXT sy-vline
WA_BAPI2097MOVE_STR-PRIORITY sy-vline
WA_BAPI2097MOVE_STR-SCHEDUL_TYPE sy-vline
WA_BAPI2097MOVE_STR-VISITCATEGORY sy-vline
WA_BAPI2097MOVE_STR-TRANSPORT_TYPE sy-vline
WA_BAPI2097MOVE_STR-ACCIDENT_3RDPTY sy-vline
WA_BAPI2097MOVE_STR-GEN_EVENT sy-vline
WA_BAPI2097MOVE_STR-SURGERY_TYPE sy-vline
WA_BAPI2097MOVE_STR-CLASS sy-vline
WA_BAPI2097MOVE_STR-REF_PSTTRT_TYPE sy-vline
WA_BAPI2097MOVE_STR-SPECIALTY sy-vline
WA_BAPI2097MOVE_STR-ARRIVAL_MODE sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_PRIO sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_TYPE sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_DELDAT sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_DELREAS sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_STATUS sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_HOSP sy-vline
WA_BAPI2097MOVE_STR-WAITLIST_INCLON sy-vline
WA_BAPI2097MOVE_STR-CREATION_DATE sy-vline
WA_BAPI2097MOVE_STR-CREATION_USER sy-vline
WA_BAPI2097MOVE_STR-UPDATE_DATE sy-vline
WA_BAPI2097MOVE_STR-UPDATE_USER sy-vline
WA_BAPI2097MOVE_STR-CANCEL_IND sy-vline
WA_BAPI2097MOVE_STR-CANCEL_USER sy-vline
WA_BAPI2097MOVE_STR-CANCEL_DATE sy-vline
WA_BAPI2097MOVE_STR-CANCEL_REASON sy-vline
WA_BAPI2097MOVE_STR-TREAT_CODE sy-vline
WA_BAPI2097MOVE_STR-CREATION_TIME sy-vline
WA_BAPI2097MOVE_STR-PRIORITY_LONG sy-vline
WA_BAPI2097MOVE_STR-EXT_MOVEMENT_ID sy-vline
WA_BAPI2097MOVE_STR-SAPS sy-vline
WA_BAPI2097MOVE_STR-REF_HOSPITAL_NAME2 sy-vline
WA_BAPI2097MOVE_STR-REF_HOSPITAL_NAME3 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.