CREATE DATA - TYPE, LIKE

Syntax

CREATE DATA dref [area_handle]
                 { {TYPE [LINE OF] {type|(name)}}
                 | {LIKE [LINE OF] dobj} }.


Effect

type can be any data type from the ABAP Dictionary - especially the structure of a database table, a public data type of a global class, or any data type of the same program that has already been defined with TYPES and that is either more specific than or identical to the static type of dref. Alternatively, you can specify a character-type data object name in parentheses which contains the name of the existing data type when the statement is executed. You have to specify name in upper case prior to Release 6.10. The identifier in name can also be an absolute type name. If a standard table type with a generic table key is specified after TYPE, a new bound table type with standard key is generated and used.

For dobj, a data object that is visible at this point can be specified. The generated data object copies this data type. Within a procedure, a generically typed formal parameter can also be specified for dobj. When the procedure is executed, the generated data object copies the current data type of the formal parameter.

You can use the optional addition LINE OF if type or the name in name is a table type, or if dobj is an internal table. As a result, the generated data object copies the attributes of the row type of the internal table. Note that you cannot combine LINE OF and the dynamic specification in name prior to release 6.10.

Note

Apart from data objects in the same program, data objects that can be referred to using LIKE also include the public attributes of global classes.

Example

Generation of a work area for any database table and import of the first rows of that database table into the work area using a SELECT loop. Because the data reference dref has a dynamic type, you can use the field symbol <wa> to access the work area. An addition field symbol is required for the dynamic access to the individual components.

PARAMETERS: dbtab TYPE c LENGTH 10,
            rows  TYPE i DEFAULT 100.

DATA dref TYPE REF TO data.

FIELD-SYMBOLS: <wa> TYPE ANY,
               <comp> TYPE ANY.

TRY.
    CREATE DATA dref TYPE (dbtab).
    ASSIGN dref->* TO <wa>.
    SELECT *
           FROM (dbtab) UP TO rows ROWS
           INTO <wa>.
      DO.
        ASSIGN COMPONENT sy-index
               OF STRUCTURE <wa> TO <comp>.
        IF sy-subrc = 0.
          WRITE / <comp>.
        ELSE.
          EXIT.
        ENDIF.
      ENDDO.
      ULINE.
    ENDSELECT.
  CATCH cx_sy_create_data_error.
    WRITE 'Wrong Database!'.
ENDTRY.