Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Creating Objects and Values →  CREATE DATA →  CREATE DATA - TABLE OF 

Creating Tabular Data Objects

This example demonstrates how tabular data objects are created.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA dref TYPE REF TO data.

    DATA: dbtab TYPE tabname VALUE 'SPFLI',
          rows  TYPE i VALUE 100.

    FIELD-SYMBOLS <table> TYPE ANY TABLE.

    cl_demo_input=>new(
      )->add_field( CHANGING field = dbtab
      )->add_field( CHANGING field = rows )->request( ).

    DATA(out) = cl_demo_output=>new( ).

    dbtab = to_upper( dbtab ).
    TRY.
        cl_abap_dyn_prg=>check_table_name_str(
          val = dbtab
          packages = 'SAPBC_DATAMODEL' ).
      CATCH cx_abap_not_a_table.
        out->display( 'Database table not found' ).
        LEAVE PROGRAM.
      CATCH cx_abap_not_in_package.
        out->display(
          'Only tables from the flight data model are allowed' ).
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        CREATE DATA dref TYPE STANDARD TABLE OF (dbtab)
                              WITH NON-UNIQUE DEFAULT KEY.
        ASSIGN dref->* TO <table>.
        SELECT *
               FROM (dbtab) UP TO @rows ROWS
               INTO TABLE @<table>.
        out->display( <table> ).
      CATCH cx_sy_create_data_error.
        out->display( 'Error in data creation' ).
    ENDTRY.

Description

Creates an internal table that matches any database table and reads the first few rows (rows) of the database into the internal table. Since the data reference dref is typed dynamically, the internal table can only be read using the field symbol <tab>.

The method CHECK_TABLE_NAME_STR of the class CL_ABAP_DYN_PRG checks whether the database table specified exists and can be used.