Skip to content

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

Creating Tabular Data Objects

The example demonstrates the creation of tabular data objects

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

Creating an internal table that matches any database table and reading the first rows (rows) of the database into the internal table. Since the dref data reference is dynamically typed, access to the internal table can only take place using the <tab> field symbol.

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

See also the executable example for SELECT INTO NEW, where the internal table is created implicitly in the INTO clause.