SAP UNICODE ASSIGN VARIANTS

Get Example source ABAP code based on a different SAP table
  


ARTICLE

Assignments to Field Symbols
Overview
1 RANGE
2 INCREMENT
3 CASTING Until now, the ASSIGN statement allowed you to define addresses beyond the boundaries of fields by specifying the offset or length. Runtime errors did not occur unless you addressed data beyond the boundaries of the data segment. With Unicode, however, problems occur since it is not possible to ensure that cross-field offset or length definitions can be interpreted as bytes or characters in a standardized and meaningful manner in both Unicode and non-Unicode systems. For this reason, special rules apply to accesses using offset and length addressing in Unicode programs. These are described in more detail in Accesses Using Offset and Length Addressing In addition, the ASSIGN statement was extended to include the RANGE and INCREMENT additions while the CASTING addition now supports all variants of this statement. The RANGE addition is offered for all valid variants of ASSIGN and can be combined with the CASTING addition.

1 RANGE ASSIGN feld1 TO <(><)> RANGE feld2. This addition explicitly sets the range limits, making it possible to define addresses past field limits, for example to edit repeating groups with the ASSIGN INCREMENT statement.
The field limits of are used as the range for <(><)> .
In a Unicode program, the limits specified by the RANGE definition must include the range limits that would otherwise result from the rules described under Offset and Length Accessing with Field Symbols.

If the memory area of field1 is not completely contained in field2, an exception that can be handled occurs.
The field field2, which defines the range, may also be deep. Repeating groups with deep types therefore can also be edited.

2 INCREMENT ASSIGN feld INCREMENT n TO <(><<)>f>. The field symbol is incremented by n times the length of field, starting with the position defined by field. First the memory range for the access is defined from the length of field and the INCREMENT specification. The addressed range must lie within the range limits. As for ASSIGN, the range for the check is defined using offset and length specifications. If it is not possible to make the assignment because the range limits were violated, SY-SUBRC is set to > 0 and the field symbol is not changed. The range limits for ASSIGN field INCREMENT n TO are defined in exactly the same way as for ASSIGN field TO f. The definition of the INCREMENT therefore has no effect on the definition of the range limits.
Example: * Loop through an elementary field
DATA: c(10) TYPE C VALUE 'abcdefghij'.
FIELD-SYMBOLS: <(><<)>cf> TYPE C.
ASSIGN c(2) TO <(><<)>cf>.
DO 5 TIMES.
WRITE / <(><<)>cf>.
ASSIGN <(><<)>cf> INCREMENT 1 TO c.
ENDDO.
Example: *Structured repeating group
TYPES: BEGIN OF comp,
f1 type string,
...
END OF comp.
DATA: BEGIN OF stru,
x1(1) TYPE x,
k1 TYPE comp,
k2 TYPE comp,
k3 TYPE comp,
k4 TYPE comp,
END OF stru.
FIELD-SYMBOLS: TYPE comp.
ASSIGN stru-k1 TO RANGE stru.
* Range-Grenzen feldübergreifend festlegen
DO 4 TIMES.
...
ASSIGN INCREMENT 1 TO .
ENDDO.
Example: * Dynamic access to an element of a repeating group
DATA: BEGIN OF stru,
x1(1) TYPE x,
k1 TYPE comp,
k2 TYPE comp,
k3 TYPE comp,
k4 TYPE comp,
END OF stru,
incr TYPE i.
FIELD-SYMBOLS: TYPE comp.

incr = 4 - 1.
ASSIGN stru-k1 INCREMENT incr TO RANGE stru.
...
* is now set to stru-k4

3 CASTING

ASSIGN feld TO CASTING.
ASSIGN feld TO <(><<)>fs> CASTING TYPE type.
ASSIGN feld TO <(><<)>fs> CASTING TYPE (typename).
ASSIGN feld TO <(><<)>fs> CASTING LIKE fld.
ASSIGN feld TO <(><<)>fs> CASTING DECIMALS dec. You can use ASSIGN ... CASTING to treat the contents of a field as a value of another type using a field symbol. One way of using this statement would be to provide different views on a structure with casts on different types. One wide-spread ABAP technique is to use C fields or structures as containers for storing structures of different types that are frequently only known at runtime. The components of the structure are selected with offset/length accesses to the container. Since this technique no longer works with Unicode, you can also look upon an existing memory area as a container with the suitable type definition using a field symbol with the ASSIGN ... CASTING statement. In the next example, the content of a selected field in a database is read, whereby the field and table names are only defined at runtime.
Example: PARAMETERS:
tab_name TYPE tname, 'Table name
tab_comp TYPE tfieldname, 'Fieldname
line_num TYPE i DEFAULT 10. 'Line number
DATA:
BEGIN OF BUFFER,
alignment TYPE F, 'Alignment
c(8000) TYPE C, 'Table content
END OF BUFFER.
FIELD-SYMBOLS:
<(><)> TYPE ANY,
<(><)> TYPE ANY.

ASSIGN BUFFER TO <(><)> CASTING TYPE (tab_name).
SELECT * FROM (tab_name) INTO <(><)>.
CHECK sy-dbcnt < line_num.
ASSIGN COMPONENT tab_comp OF STRUCTURE <(><)> TO <(><)>.
WRITE: / tab_comp, <(><)>.
ENDSELECT.
Note
Until now, in the ASSIGN feld TO <(><)> CASTING ... statement, the system checked to ensure that the field was at least as long as the type that was assigned to the field symbol, <(> <)>. (Field symbols can either be typed at declaration or the type specified in an ASSIGN statement using CASTING TYPE). The syntax check is now more thorough. Now, you can only assign the field field (in either a Unicode or non-Unicode program)
provided it is at least as long as the type assigned to the field symbol <(><)>. Otherwise, the system returns a syntax error. At runtime, the system only checks to see whether or not the lengths are compatible in the current system (as before). If the field type or field symbol type is a deep structure, the system also checks that the offset and type of all the reference components match in the area of field that is covered by <(><)>. The syntax check is now more thorough. Now, the system checks that these components must be compatible with all systems, whether they have a one-byte, double-byte, or four-byte character length. At runtime, the system only checks to see whether or not the reference components are compatible in the current system In Unicode systems, in the ASSIGN str TO <(><)> TYPE C/N and ASSIGN str TO <(><)> CASTING TYPE C/N statements, the length of str may not always be a multiple of the character length, in which case the program terminates at runtime.