Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  CREATE DATA →  CREATE DATA - TYPE, LIKE 

Creating Structured Data Objects

This example demonstrates how structured 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 <wa> TYPE any.

    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.
        dbtab = 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 (dbtab).
        ASSIGN dref->* TO <wa>.
        SELECT *
               FROM (dbtab) UP TO @rows ROWS
               INTO @<wa>.
          out->write( <wa> ).
        ENDSELECT.
        out->display( ).
      CATCH cx_sy_create_data_error.
        out->display( 'Error in data creation' ).
    ENDTRY.

Description

Creates a work area that is compatible with a database table and reads the first few rows (rows) of the database table into this work area using a SELECT loop. Since the data reference dref is typed dynamically, the work area can only be read using the field symbol <wa>.

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 structure is created implicitly in the INTO clause.