Skip to Content
avatar image
Former Member

Dynamic Data Declaration

Hi Guys

Could any one tell me how can we declare data variables at runtime in ABAP.

Good replies ll be appreciated.

Regards

Rakesh.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Dec 21, 2005 at 12:36 PM

    CREATE - Creating a General Data Object

    Variants:

    1. CREATE DATA dref.

    2. CREATE DATA dref TYPE simpletype .

    3. CREATE DATA dref LIKE simpleobj .

    4. CREATE DATA dref TYPE tabkind OF linetype

    [WITH [UNIQUE|NON-UNIQUE] keydef] [INITIAL SIZE n].

    5. CREATE DATA dref LIKE tabkind OF lineobj

    [WITH [UNIQUE|NON-UNIQUE] keydef] [INITIAL SIZE n].

    Effect

    Creates a new anonymous data object with the specified type. Once it has been created, the data object contains the appropriate initial value for its type and the logical expression dref IS BOUND is true. dref must be a general reference variable with the type REF TO DATA

    Once you have executed the statement, the data object exists in the current internal session, and the reference in dref points to the data object. If you want to access the contents of the data object, you must use ASSIGN to assign it to a field symbol.

    In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.See Creating Data Objects Dynamically

    Variant 1

    CREATE DATA dref.

    This short form, without further additions, is only allowed for completely typed data references, that is references whose complete reference type is known statically. The system creates a data object with the statically specified reference type.

    Example

    In the following example a data object with the type SBOOK is dynamically created and dereferenced for use:

    data DREF type ref to SBOOK.

    create data DREF.

    select *

    from SBOOK

    into DREF->*.

    ...

    endselect.

    Variant 2

    CREATE DATA dref TYPE simpletype.

    Effect

    For a simple type specification you can use the REF TO or LINE OF additions to specify a reference type or line type of a table type resepectively. If you are declaring an elementary type, you can also use the LENGTH and DECIMALS additions. The type specification must be compatible with the reference variable type.

    Example

    In the following example, we create an anonymous data object to which the reference dref points. The type of the data object is the type of the work area in the database table SBOOK. We then assign the data object to the field symbol <fs> using the dereferencing operator ->*. Any other dereferencing is not possible here, because the reference variable is not completely typed. You can now work with the fieldsymbol as normal.

    DATA: dref TYPE REF TO DATA.

    FIELD-SYMBOLS: <fs> TYPE ANY.

    " Create object of type SBOOK and attach the field symbol

    CREATE DATA dref TYPE sbook.

    ASSIGN dref->* TO <fs>.

    Variant 3

    CREATE DATA dref LIKE simpleobj.

    Effect

    Unlike variant 2, you declare the type using areference to an existing data object. After simply declaring the object, you can also reference the line type of an internal table using the LINE OF addition.

    Example

    In the following example, we reference the line type of the generic table parameter Itab.

    FORM MyForm USING Itab TYPE ANY TABLE.

    DATA: LineRef TYPE REF TO DATA.

    FIELD-SYMBOLS: <fs> TYPE ANY.

    " Get line type dynamically and attach the field-symbol

    CREATE DATA LineRef LIKE LINE OF Itab.

    ASSIGN LineRef->* TO <fs>.

    ...

    ENDFORM.

    Variant 4

    CREATE DATA dref TYPE tabkind OF linetype

    [WITH [UNIQUE|NON-UNIQUE] keydef]

    [INITIAL SIZE n].

    Effect

    Creates an internal table at runtime. You define the table by declaring the table kind, line type, whether the key is unique or non-unique, the key, and initial number of lines.

    You must declare the table kind statically. Since a data object must always be completely typed, the table kinds INDEX KIND and ANY KIND are not permitted. You can declare the line type statically or dynamically.

    You can omit the key definition for STANDARD tables. If you do, the system uses the standard key. You can declare the key components statically or dynamically (as the contents of a table with component names). You can also declare the initial number of lines in the table either statically or dynamically (as the contents of a variable).

    Examples

    In the following example, we create a standard table with a dynamically declared line type. The field symbol that points to the newly created table must have an appropriate table type (which can be generic, if necessary).

    DATA: LineType TYPE string,

    ItabRef TYPE REF TO DATA.

    FIELD-SYMBOLS: TYPE STANDARD TABLE.

    ...

    LineType = 'SFLIGHT'.

    ...

    " Create internal table and attach a field-symbol

    CREATE DATA ItabRef TYPE STANDARD TABLE OF (LineType).

    ASSIGN ItabRef->* TO .

    The following example shows you how to define a table of the type HASHED TABLE, whose line type, key components, and INITIAL SIZE are unknown until runtime.

    DATA: LineType TYPE string,

    KeyTab TYPE TABLE OF string,

    ItabRef TYPE REF TO DATA,

    n TYPE i.

    FIELD-SYMBOLS: TYPE HASHED TABLE.

    ...

    LineType = 'SFLIGHT'.

    APPEND 'CARRID' TO KeyTab.

    APPEND 'CONNID' TO KeyTab.

    n = 10.

    ...

    " Create hashed table and attach a field-symbol

    CREATE DATA ItabRef TYPE HASHED TABLE OF (LineType)

    WITH UNIQUE KEY (KeyTab)

    INITIAL SIZE n.

    ASSIGN ItabRef->* TO .

    Variant 5

    CREATE DATA dref LIKE tabkind OF lineobj

    [WITH [UNIQUE|NON-UNIQUE] keydef]

    [INITIAL SIZE n].

    Effect

    As for variant 4, except that the line type is declared with reference to an existing data object.

    Exceptions

    Catchable Exceptions

    CX_SY_CREATE_DATA_ERROR

    Cause: Invalid value for the DECIMALS addition.

    Runtime Error: CREATE_DATA_ILLEGAL_DECIMALS (catchable)

    Cause: Invalid value for the INITIAL SIZE addition.

    Runtime Error: CREATE_DATA_ILLEGAL_INIT_SIZE (catchable)

    Cause: Invalid value for the LENGTH addition.

    Runtime Error: CREATE_DATA_ILLEGAL_LENGTH (catchable)

    Cause: The LENGTH addition was used for a type other than C, N, X or P.

    Runtime Error: CREATE_DATA_LEN_NOT_ALLOWED (catchable)

    Cause: The type specified dynamically in the TYPE is not completely typed.

    Runtime Error: CREATE_DATA_NOT_ALLOWED_TYPE (catchable)

    Cause: The type specified in the TYPE addition is unknown.

    Runtime Error: CREATE_DATA_UNKNOWN_TYPE (catchable)

    Non-Catchable Exceptions

    Cause: The variable dref is not of the right type.

    Runtime Error: CREATE_DATA_REFERENCE_EXPECTED

    Related

    GET REFERENCE, FIELD-SYMBOLS, ASSIGN, DATA

    GET REFERENCE

    Basic form 11

    GET REFERENCE OF f INTO dref.

    Effect

    Creates a reference dref to a data object (field) f.

    The reference to f is written to the reference variable ref. dref can be any reference variable with type REF TO DATA or REF TO type. For typed reference variables, the data type of f must be compatible with the type of the reference variables. If the referencing is successful, the logical expression dref IS BOUND is true.

    Example

    DATA: dref TYPE REF TO DATA,

    booking TYPE sbook.

    FIELD-SYMBOLS: <fs> TYPE ANY.

    GET REFERENCE OF booking INTO dref.

    ASSIGN dref->* TO <fs>.

    This example creates a reference dref to the data object booking. The data object is then assigned to the field symbol <fs> using the dereferencing operator ->*. Since dref is generic, this is the only type of dereferencing.

    Exceptions

    Non-Catchable Exceptions

    Cause: The data object after INTO is not a reference variable.

    Runtime Error: GET_REF_REFERENCE_EXPECTED

    Cause: GET REFERENCE is not allowed for substrings.

    Runtime Error: GET_REF_SUBSTRING_NOT_ALLOWED

    Related

    CREATE DATA, FIELD-SYMBOLS, ASSIGN

    Notes

    References to local variables are invalid once you leave the environment of the local variable.

    Non-Catchable Exceptions

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 21, 2005 at 12:38 PM

    hi

    use <b>parameter</b>

    eg)

    parameter:r type i.

    regards

    Arun

    Add comment
    10|10000 characters needed characters exceeded