SAP TYP DATA REF

Get Example source ABAP code based on a different SAP table
  


ARTICLE

Typed Data References - Overview

Using Typed Data References
For fully generic references, you can only edit the created data objects if you use field symbols. However, in the case of typed data references, you can directly address the components of the data object referenced and therefore use them at any operand position. data DREF type ref to SFLIGHT.
create data DREF.
DREF->FLDATE = DREF->FLDATE + 5.
if DREF->SEATSMAX > 10.
write: / DREF->FLDATE, DREF->SEATSMAX.
endif.
Typing requires that the type of the actual object to which you want to define the reference is known. If this is not the case as, for example, with containers, the only way to access the object is to use the fully generic technique using field symbols.

Advantages of Typing
A great advantage provided by typed data references is that data areas are adjusted to the actual requirements. If, for example, you do not know exactly whether a specific field is needed at all in a certain structure, you define a data reference instead of a field and then use CREATE DATA to create the corresponding data object only if the field is addressed. This way, memory requirements at runtime can be reduced considerably. types: begin of STRUC_1,
A type I,
B type ref to SFLIGHT,
C type P,
end of STRUC_1.
data: S1 type STRUC_1.
...
if S1-A > 10.
create data S1-B.
S1-B->CARRID = 'LH'.
...
S1-B->PAYMENTSUM = 1000.
endif.
Another advantage as compared with generic data references is that the entire data object can be directly addressed at any operand position using the operator ->*. The operator ->* can also be used to create reference chains as shown in the following examples: types: begin of STRUC_2,
X(10) type C,
Y type ref to STRUC_1,
Z type I,
end of STRUC_2.
data: S2 type STRUC_2,
R2 type ref to STRUC_2.
...
S2-Y->A = 100.
S2-Y->*-A = 200. 'Same as S2-Y->A ...
S2-Y->B->FLDATE = SY-DATUM.

Assigning Reference Variables

Assignments between reference variables of the same type
Typed data reference variables can be assigned to one another if the static types of the source and target variables are the same:
If the static type is elementary, the types are the same if all their technical attributes match.
If the static type is not elementary, the types are only the same if (as for object references) both data types are declared with reference to exactly the same data type (identified by the name of the type using TYPE REF TO or the data object using LIKE REF TO).

Narrowing und Widening Casts
Like with object references, you can always assign a typed reference to a generic reference. The reverse is only possible using the operator ?=. data: DREF_1 type ref to data,
DREF_2 type ref to SFLIGHT.
...
DREF_2 = DREF_1. 'Syntax error

create data DREF_1 type SFLIGHT.
DREF_2 ?= DREF_1. 'OK

create data DREF_1 type SPFLI.
DREF_2 ?= DREF_1. 'Exception
The exception in the last case can be handled between TRY ...ENDTRY.

Type Check of Reference Parameters
You may only pass a typed reference to a generic parameter typed with reference to REF TO DATA if the parameter is an IMPORTING parameter. In particular, you cannot pass a typed reference to a CHANGING or EXPORTING parameter, as this would violate the above Narrowing Cast rules.

Internal Tables
Like the ASSIGNING addition for internal tables, you can use the addition REFERENCE INTO dref to place a reference to a table line into the field dref. You can then use this reference to directly operate on the contents of the internal table. The addition REFERENCE INTO dref is allowed in the following statements: LOOP, READ, APPEND, INSERT, MODIFY, and COLLECT.
As a rule, the reference is only set with this addition if the statement is executed successfully. In the LOOP statement, you must not change the reference variable in the loop. When the loop processing is finished, the reference variable points to the table line that was last edited.