Start of Content Area

Creating Data Objects Dynamically  Locate the document in its SAP Library structure

The CREATE DATA statement prior to Release 6.10

CREATE DATA allows you to create fields in a pre-defined or user-defined data type. The statement has the following variants:

CREATE DATA dref TYPE  typ.
CREATE DATA dref TYPE (typname).
CREATE DATA dref LIKE  feld.
CREATE DATA dref TYPE LINE OF itab.
CREATE DATA dref LIKE LINE OF itab.

In the following example, a specific field is read from database table X031L. Note that neither the field name nor the table name is known until runtime:

Example

* Read a field from the table X031L

PARAMETERS:
  TAB_NAME    LIKE SY-TNAME,           "Table name
  TAB_COMP    LIKE X031L-FIELDNAME,   "Field name
  ANZAHL      TYPE I DEFAULT 10.       Number of lines

FIELD-SYMBOLS: <WA>   TYPE ANY,
               <COMP> TYPE ANY.

  DATA: WA_REF TYPE REF TO DATA.

  CREATE DATA WA_REF TYPE (TAB_NAME). "Suitable work area
  ASSIGN WA_REF->* TO <WA>.

SELECT * FROM (TAB_NAME) INTO <WA>
  UP TO anzahl ROWS.
  WRITE: / TAB_COMP, <COMP>.
  ENDSELECT.

The CREATE DATA statement for internal tables

Another variant of CREATE DATA allows you to create table objects at runtime. The line type and table key can be entered statically or dynamically.

CREATE DATA dref (TYPE [STANDARD|SORTED|HASHED] TABLE
     OF (LineType | (Name) |REF TO DATA | REF TO Obj))
     | (LIKE [STANDARD|SORTED|HASHED] TABLE OF LineObj
      
[ WITH (UNIQUE|NON-UNIQUE)
        (KEY (k1 ... kn | (keytab) | TABLE_LINE )| DEFAULT KEY )  ]
       [ INITIAL SIZE m ]

The following constraints apply to this variant:

  1. m is a variable or a constant without a sign, whose content at runtime must be of the type NUMLIKE.
  2. keytab is a table of the type CHARLIKE, which must not be empty, and whose components must not contain any offset, length, or overlapping key entries. You can use the TABLE_LINE addition, if the table contains only one line.
  3. The system returns a syntax error if either the type, or line declaration and the key declaration are static.
  4. If you do not define a key, the system uses the DEFAULT-KEY.

The CREATE DATA statement with built-in generic types

You can also use the basic generic types, C, N, X, and P with the CREATE DATA statement. You can specify the length and number of decimal places (for type P) using additions.

CREATE DATA dref TYPE (t | (typeName))
[ LENGTH len ]
[ DECIMALS dec ].

You can only use the LENGTH addition for types C, N, X, and P and you must always include it after the TYPE keyword. A catchable runtime error occurs if you do not comply with syntax conventions when entering LENGTH or DECIMALS values.

 

 

 

 

 

 

End of Content Area