ABAP Select data from SAP table /SDF/SYB_STMT_DATA 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 /SDF/SYB_STMT_DATA 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 /SDF/SYB_STMT_DATA. 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 /SDF/SYB_STMT_DATA 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_/SDF/SYB_STMT_DATA TYPE STANDARD TABLE OF /SDF/SYB_STMT_DATA,
      WA_/SDF/SYB_STMT_DATA TYPE /SDF/SYB_STMT_DATA,
      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: </SDF/SYB_STMT_DATA> TYPE /SDF/SYB_STMT_DATA.

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

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM /SDF/SYB_STMT_DATA
  INTO TABLE IT_/SDF/SYB_STMT_DATA.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM /SDF/SYB_STMT_DATA
*  INTO TABLE @DATA(IT_/SDF/SYB_STMT_DATA2).
*--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_/SDF/SYB_STMT_DATA INDEX 1 INTO DATA(WA_/SDF/SYB_STMT_DATA2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/SDF/SYB_STMT_DATA ASSIGNING </SDF/SYB_STMT_DATA>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</SDF/SYB_STMT_DATA>-DL_ID = 1.
</SDF/SYB_STMT_DATA>-ANALYSIS_ID = 1.
</SDF/SYB_STMT_DATA>-MATCHED_ANALYSIS_ID = 1.
</SDF/SYB_STMT_DATA>-SORT_ORDER = 1.
</SDF/SYB_STMT_DATA>-INSTANCEID = 1.
ENDLOOP.

LOOP AT IT_/SDF/SYB_STMT_DATA INTO WA_/SDF/SYB_STMT_DATA.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/SDF/SYB_STMT_DATA-SQL_ID, sy-vline,
WA_/SDF/SYB_STMT_DATA-HKEY, sy-vline,
WA_/SDF/SYB_STMT_DATA-USERID, sy-vline,
WA_/SDF/SYB_STMT_DATA-SUSERID, sy-vline,
WA_/SDF/SYB_STMT_DATA-DBNAME, sy-vline,
WA_/SDF/SYB_STMT_DATA-STMTTYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SDF/SYB_STMT_DATA 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_/SDF/SYB_STMT_DATA 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_/SDF/SYB_STMT_DATA INTO WA_/SDF/SYB_STMT_DATA. *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_/SDF/SYB_STMT_DATA_STR,
DL_ID TYPE STRING,
ANALYSIS_ID TYPE STRING,
MATCHED_ANALYSIS_ID TYPE STRING,
SORT_ORDER TYPE STRING,
INSTANCEID TYPE STRING,
SQL_ID TYPE STRING,
HKEY TYPE STRING,
USERID TYPE STRING,
SUSERID TYPE STRING,
DBNAME TYPE STRING,
STMTTYPE TYPE STRING,
AUTOPARAMS TYPE STRING,
PARALLELDEGREE TYPE STRING,
QUOTEDIDENTIFIER TYPE STRING,
TRANSACTIONISOLATIONLEVEL TYPE STRING,
TRANSACTIONMODE TYPE STRING,
SAAUTHORIZATION TYPE STRING,
SYSTEMCATALOGUPDATE TYPE STRING,
CACHEDDATE TYPE STRING,
OPTIMIZATIONGOAL TYPE STRING,
OPTIMIZERLEVEL TYPE STRING,
USECOUNT TYPE STRING,
STATEMENTSIZE TYPE STRING,
MINPLANSIZEKB TYPE STRING,
MAXPLANSIZEKB TYPE STRING,
CURRENTUSAGECOUNT TYPE STRING,
MAXUSAGECOUNT TYPE STRING,
NUMRECOMPILESSCHEMACHANGES TYPE STRING,
NUMRECOMPILESPLANFLUSHES TYPE STRING,
METRICSCOUNT TYPE STRING,
MINPIO TYPE STRING,
MAXPIO TYPE STRING,
AVGPIO TYPE STRING,
MINLIO TYPE STRING,
MAXLIO TYPE STRING,
AVGLIO TYPE STRING,
MINCPUTIME TYPE STRING,
MAXCPUTIME TYPE STRING,
AVGCPUTIME TYPE STRING,
MINELAPSEDTIME TYPE STRING,
MAXELAPSEDTIME TYPE STRING,
AVGELAPSEDTIME TYPE STRING,
TOTALELAPSEDTIME TYPE STRING,
TOTALCPUTIME TYPE STRING,
TOTALLIO TYPE STRING,
TOTALPIO TYPE STRING,
ROWS_READ_PROCESSED TYPE STRING,
LIO_ROWS_PROCESSED TYPE STRING,
PIO_ROWS_PROCESSED TYPE STRING,
LASTUSEDDATE TYPE STRING,
AVGSCANROWS TYPE STRING,
MAXSCANROWS TYPE STRING,
AVGQUALIFYINGREADROWS TYPE STRING,
MAXQUALIFYINGREADROWS TYPE STRING,
AVGQUALIFYINGWRITEROWS TYPE STRING,
MAXQUALIFYINGWRITEROWS TYPE STRING,
LOCKWAITS TYPE STRING,
LOCKWAITTIME TYPE STRING,
SORTCOUNT TYPE STRING,
SORTSPILLEDCOUNT TYPE STRING,
TOTALSORTTIME TYPE STRING,
MAXSORTTIME TYPE STRING,
PARALLELDEGREEREDUCED TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SDF/SYB_STMT_DATA_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_/SDF/SYB_STMT_DATA_STR-DL_ID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-ANALYSIS_ID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MATCHED_ANALYSIS_ID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SORT_ORDER sy-vline
WA_/SDF/SYB_STMT_DATA_STR-INSTANCEID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SQL_ID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-HKEY sy-vline
WA_/SDF/SYB_STMT_DATA_STR-USERID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SUSERID sy-vline
WA_/SDF/SYB_STMT_DATA_STR-DBNAME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-STMTTYPE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AUTOPARAMS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-PARALLELDEGREE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-QUOTEDIDENTIFIER sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TRANSACTIONISOLATIONLEVEL sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TRANSACTIONMODE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SAAUTHORIZATION sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SYSTEMCATALOGUPDATE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-CACHEDDATE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-OPTIMIZATIONGOAL sy-vline
WA_/SDF/SYB_STMT_DATA_STR-OPTIMIZERLEVEL sy-vline
WA_/SDF/SYB_STMT_DATA_STR-USECOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-STATEMENTSIZE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MINPLANSIZEKB sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXPLANSIZEKB sy-vline
WA_/SDF/SYB_STMT_DATA_STR-CURRENTUSAGECOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXUSAGECOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-NUMRECOMPILESSCHEMACHANGES sy-vline
WA_/SDF/SYB_STMT_DATA_STR-NUMRECOMPILESPLANFLUSHES sy-vline
WA_/SDF/SYB_STMT_DATA_STR-METRICSCOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MINPIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXPIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGPIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MINLIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXLIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGLIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MINCPUTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXCPUTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGCPUTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MINELAPSEDTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXELAPSEDTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGELAPSEDTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TOTALELAPSEDTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TOTALCPUTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TOTALLIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TOTALPIO sy-vline
WA_/SDF/SYB_STMT_DATA_STR-ROWS_READ_PROCESSED sy-vline
WA_/SDF/SYB_STMT_DATA_STR-LIO_ROWS_PROCESSED sy-vline
WA_/SDF/SYB_STMT_DATA_STR-PIO_ROWS_PROCESSED sy-vline
WA_/SDF/SYB_STMT_DATA_STR-LASTUSEDDATE sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGSCANROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXSCANROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGQUALIFYINGREADROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXQUALIFYINGREADROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-AVGQUALIFYINGWRITEROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXQUALIFYINGWRITEROWS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-LOCKWAITS sy-vline
WA_/SDF/SYB_STMT_DATA_STR-LOCKWAITTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SORTCOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-SORTSPILLEDCOUNT sy-vline
WA_/SDF/SYB_STMT_DATA_STR-TOTALSORTTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-MAXSORTTIME sy-vline
WA_/SDF/SYB_STMT_DATA_STR-PARALLELDEGREEREDUCED sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.