ABAP Select data from SAP table ORA_DBA_TABLES 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 ORA_DBA_TABLES 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 ORA_DBA_TABLES. 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 ORA_DBA_TABLES 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_ORA_DBA_TABLES TYPE STANDARD TABLE OF ORA_DBA_TABLES,
      WA_ORA_DBA_TABLES TYPE ORA_DBA_TABLES,
      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: <ORA_DBA_TABLES> TYPE ORA_DBA_TABLES.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM ORA_DBA_TABLES
*  INTO TABLE @DATA(IT_ORA_DBA_TABLES2).
*--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_ORA_DBA_TABLES INDEX 1 INTO DATA(WA_ORA_DBA_TABLES2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_ORA_DBA_TABLES ASSIGNING <ORA_DBA_TABLES>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<ORA_DBA_TABLES>-SNAPSHOT_ID = 1.
<ORA_DBA_TABLES>-OWNER = 1.
<ORA_DBA_TABLES>-TABLE_NAME = 1.
<ORA_DBA_TABLES>-TABLESPACE_NAME = 1.
<ORA_DBA_TABLES>-CLUSTER_NAME = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_ORA_DBA_TABLES-IOT_NAME, sy-vline,
WA_ORA_DBA_TABLES-PCT_FREE, sy-vline,
WA_ORA_DBA_TABLES-PCT_USED, sy-vline,
WA_ORA_DBA_TABLES-INI_TRANS, sy-vline,
WA_ORA_DBA_TABLES-MAX_TRANS, sy-vline,
WA_ORA_DBA_TABLES-INITIAL_EXTENT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_ORA_DBA_TABLES 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_ORA_DBA_TABLES 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_ORA_DBA_TABLES INTO WA_ORA_DBA_TABLES. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_ORA_DBA_TABLES_STR,
SNAPSHOT_ID TYPE STRING,
OWNER TYPE STRING,
TABLE_NAME TYPE STRING,
TABLESPACE_NAME TYPE STRING,
CLUSTER_NAME TYPE STRING,
IOT_NAME TYPE STRING,
PCT_FREE TYPE STRING,
PCT_USED TYPE STRING,
INI_TRANS TYPE STRING,
MAX_TRANS TYPE STRING,
INITIAL_EXTENT TYPE STRING,
NEXT_EXTENT TYPE STRING,
MIN_EXTENTS TYPE STRING,
MAX_EXTENTS TYPE STRING,
PCT_INCREASE TYPE STRING,
FREELISTS TYPE STRING,
FREELIST_GROUPS TYPE STRING,
LOGGING TYPE STRING,
BACKED_UP TYPE STRING,
NUM_ROWS TYPE STRING,
BLOCKS TYPE STRING,
EMPTY_BLOCKS TYPE STRING,
AVG_SPACE TYPE STRING,
CHAIN_CNT TYPE STRING,
AVG_ROW_LEN TYPE STRING,
AVG_SPACE_FREE TYPE STRING,
NUM_FREELIST_BLK TYPE STRING,
DEGREE TYPE STRING,
INSTANCES TYPE STRING,
CACHE TYPE STRING,
TABLE_LOCK TYPE STRING,
SAMPLE_SIZE TYPE STRING,
LAST_ANALYZED_D TYPE STRING,
LAST_ANALYZED_T TYPE STRING,
PARTITIONED TYPE STRING,
IOT_TYPE TYPE STRING,
TEMPORARY_DBA TYPE STRING,
SECONDARY TYPE STRING,
NESTED TYPE STRING,
BUFFER_POOL TYPE STRING,
ROW_MOVEMENT TYPE STRING,
GLOBAL_STATS TYPE STRING,
USER_STATS TYPE STRING,
DURATION TYPE STRING,
SKIP_CORRUPT TYPE STRING,
MONITORING TYPE STRING,
CLUSTER_OWNER TYPE STRING,
DEPENDENCIES TYPE STRING,
COMPRESSION TYPE STRING,END OF T_EKKO_STR. DATA: WA_ORA_DBA_TABLES_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_ORA_DBA_TABLES_STR-SNAPSHOT_ID sy-vline
WA_ORA_DBA_TABLES_STR-OWNER sy-vline
WA_ORA_DBA_TABLES_STR-TABLE_NAME sy-vline
WA_ORA_DBA_TABLES_STR-TABLESPACE_NAME sy-vline
WA_ORA_DBA_TABLES_STR-CLUSTER_NAME sy-vline
WA_ORA_DBA_TABLES_STR-IOT_NAME sy-vline
WA_ORA_DBA_TABLES_STR-PCT_FREE sy-vline
WA_ORA_DBA_TABLES_STR-PCT_USED sy-vline
WA_ORA_DBA_TABLES_STR-INI_TRANS sy-vline
WA_ORA_DBA_TABLES_STR-MAX_TRANS sy-vline
WA_ORA_DBA_TABLES_STR-INITIAL_EXTENT sy-vline
WA_ORA_DBA_TABLES_STR-NEXT_EXTENT sy-vline
WA_ORA_DBA_TABLES_STR-MIN_EXTENTS sy-vline
WA_ORA_DBA_TABLES_STR-MAX_EXTENTS sy-vline
WA_ORA_DBA_TABLES_STR-PCT_INCREASE sy-vline
WA_ORA_DBA_TABLES_STR-FREELISTS sy-vline
WA_ORA_DBA_TABLES_STR-FREELIST_GROUPS sy-vline
WA_ORA_DBA_TABLES_STR-LOGGING sy-vline
WA_ORA_DBA_TABLES_STR-BACKED_UP sy-vline
WA_ORA_DBA_TABLES_STR-NUM_ROWS sy-vline
WA_ORA_DBA_TABLES_STR-BLOCKS sy-vline
WA_ORA_DBA_TABLES_STR-EMPTY_BLOCKS sy-vline
WA_ORA_DBA_TABLES_STR-AVG_SPACE sy-vline
WA_ORA_DBA_TABLES_STR-CHAIN_CNT sy-vline
WA_ORA_DBA_TABLES_STR-AVG_ROW_LEN sy-vline
WA_ORA_DBA_TABLES_STR-AVG_SPACE_FREE sy-vline
WA_ORA_DBA_TABLES_STR-NUM_FREELIST_BLK sy-vline
WA_ORA_DBA_TABLES_STR-DEGREE sy-vline
WA_ORA_DBA_TABLES_STR-INSTANCES sy-vline
WA_ORA_DBA_TABLES_STR-CACHE sy-vline
WA_ORA_DBA_TABLES_STR-TABLE_LOCK sy-vline
WA_ORA_DBA_TABLES_STR-SAMPLE_SIZE sy-vline
WA_ORA_DBA_TABLES_STR-LAST_ANALYZED_D sy-vline
WA_ORA_DBA_TABLES_STR-LAST_ANALYZED_T sy-vline
WA_ORA_DBA_TABLES_STR-PARTITIONED sy-vline
WA_ORA_DBA_TABLES_STR-IOT_TYPE sy-vline
WA_ORA_DBA_TABLES_STR-TEMPORARY_DBA sy-vline
WA_ORA_DBA_TABLES_STR-SECONDARY sy-vline
WA_ORA_DBA_TABLES_STR-NESTED sy-vline
WA_ORA_DBA_TABLES_STR-BUFFER_POOL sy-vline
WA_ORA_DBA_TABLES_STR-ROW_MOVEMENT sy-vline
WA_ORA_DBA_TABLES_STR-GLOBAL_STATS sy-vline
WA_ORA_DBA_TABLES_STR-USER_STATS sy-vline
WA_ORA_DBA_TABLES_STR-DURATION sy-vline
WA_ORA_DBA_TABLES_STR-SKIP_CORRUPT sy-vline
WA_ORA_DBA_TABLES_STR-MONITORING sy-vline
WA_ORA_DBA_TABLES_STR-CLUSTER_OWNER sy-vline
WA_ORA_DBA_TABLES_STR-DEPENDENCIES sy-vline
WA_ORA_DBA_TABLES_STR-COMPRESSION sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.