ASSIGN: ABAP Keyword a day
ASSIGN
Variants:
1. ASSIGN f TO
2. ASSIGN (f) TO
3. ASSIGN TABLE FIELD (f) TO
4. ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO
5. ASSIGN COMPONENT idx OF STRUCTURE rec TO
6. ASSIGN COMPONENT name OF STRUCTURE rec TO
Variant 1
ASSIGN f TO
Additions:
1. … TYPE typ
2. … DECIMALS dec
3. … LOCAL COPY OF …
Effect
Assigns the field f to the field symbol
Note
With the ASSIGN statement, the offset and length specifications in field f (i.e. f+off, f+len or f+off(len)) have a special
meaning:
- They may be variable and thus not evaluated until runtime.
- The system does not check whether the selected area still lies within the field f.
- If an offset is specified, but no length, for the field f, the field symbol
Caution:
- In the ASSIGN statement, you can also use offset and length specifications to access field symbols, FORM and function parameters.
- Warning: If the effect of the ASSIGN statement is to assign parts of other fields beyond the limits of the field f, the changing of the contents via the field symbol
Note
Since the ASSIGN statement does not set any return code value in the system field SY-SUBRC, subsequent program code should not read this field.
Example
DATA NAME(4) VALUE ‘JOHN’.
FIELD-SYMBOLS
ASSIGN NAME TO
WRITE
Output: JOHN
Example
DATA: NAME(12) VALUE ‘JACKJOHNCARL’,
X(10) VALUE ‘XXXXXXXXXX’.
FIELD-SYMBOLS .
ASSIGN NAME+4 TO .
WRITE .
ASSIGN NAME+4(*) TO .
WRITE .
Output: JOHNCARLXXXX JOHNCARL
Example
DATA: NAME(12) VALUE ‘JACKJOHNCARL’,
X(10) VALUE ‘XXXXXXXXXX’.
FIELD-SYMBOLS
ASSIGN NAME+4 TO
WRITE
ASSIGN NAME+4(*) TO
WRITE
Output: JOHNCARLXXXX JOHNCARL
Addition 1 … TYPE typ
Effect With untyped field symbols, allows you to change the current type of the field symbol to the type typ. The output length of the field symbol is corrected according to its type. With typed field symbols, this addition should only be used if the type of the field f does not match the type of the field symbol
Note
This statement results in a runtime error if the specified type is unknown or does not match the field to be assigned (due to a missing alignment or an inappropriate length).
Example
DATA LETTER TYPE C.
FIELD-SYMBOLS
ASSIGN LETTER TO
The field symbol has the type C and the output length 1.
ASSIGN LETTER TO
The field symbol has the type X and the output length 2.
Addition 2 … DECIMALS dec
Effect
This addition only makes sense when used with type P. The field symbol contains dec decimal places.
Example
Output sales in thousands:
DATA SALES_DEC2(10) TYPE P DECIMALS 2 VALUE 1234567.
FIELD-SYMBOLS
ASSIGN SALES_DEC2 TO
WRITE: / SALES_DEC2,
/
Output:
1,234,567.00
1,234.56700
Note
This statement results in a runtime error if the field symbol has a type other than P at runtime or the specified number of decimal places is not in the range 0 to 14.
Addition 3 … LOCAL COPY OF …
Effect
With LOCAL COPY OF, the ASSIGN statement can only be used in subroutines. This creates a copy of f which points to the field symbol.
Note
The field symbol
Example
DATA X(4) VALUE ‘Carl’.
PERFORM U.
FORM U.
FIELD-SYMBOLS
ASSIGN LOCAL COPY OF X TO
WRITE
MOVE ‘John’ TO
WRITE
WRITE X.
ENDFORM.
Output: Carl John Carl
Variant 2 ASSIGN (f) TO
Additions:
1. … TYPE typ
2. … DECIMALS dec
3. … LOCAL COPY OF …
Effect
Assigns the field whose name is stored in the field f to the field symbol.
The statement “ASSIGN (f)+off(len) TO
Notes
- The search for the field to be assigned is performed as follows:
1. If the statement is in a subroutine or function module, the system first searches in this modularization unit.
2. If the statement lies outside any such modularization units or if the field is not found there, the system searches for the field in the global data of the program.
3. If the field is not found there, the system searches in the table work areas of the main program of the current program group declared with TABLES
- The name of the field to be assigned can also be the name of a field symbol or formal parameter (or even a component of one of these, if the field symbol or the parameter has a structure).
- If the name of the field to be assigned is of the form “(program name)field name”, the system searches in the global fields of the program with the name “Program name”
for the field with the name “Field name”. However,it is only found if the program has already been loaded.
Warning:
This option is for internal use by specialists only. ncompatible changes or developments may occur at any time without warning or prior notice. The return code value is set as follows:
SY-SUBRC = 0: The assignment was successful.
SY-SUBRC = 4: The field could not be assigned to the field symbol.
Example
DATA: NAME(4) VALUE ‘XYZ’, XYZ VALUE ’5′.
FIELD-SYMBOLS
ASSIGN (NAME) TO
WRITE
Output: 5
Addition 1 … TYPE typ
Addition 2 … DECIMALS dec
Addition 3 … LOCAL COPY OF …
Effect
See similar additions of variant 1.
Variant 3
ASSIGN TABLE FIELD (f) TO
Effect
Identical to variant 2, except that the system searches for the field f only in the data in the current program group declared with TABLES. The return code value is set as follows:
SY-SUBRC = 0: The assignment was successful.
SY-SUBRC = 4: The field could not be assigned to the field symbol.
Example
TABLES TRDIR.
DATA NAME(10) VALUE ‘TRDIR-NAME’.
FIELD-SYMBOLS
MOVE ‘XYZ_PROG’ TO TRDIR-NAME.
ASSIGN TABLE FIELD (NAME) TO
WRITE
Output: XYZ_PROG
Example
TABLES T100.
T100-TEXT = ‘Global’.
PERFORM EXAMPLE.
FORM EXAMPLE.
DATA: BEGIN OF T100, TEXT(20) VALUE ‘LOCAL’, END OF T100,
NAME(30) VALUE ‘T100-TEXT’.
FIELD-SYMBOLS
ASSIGN (NAME) TO
WRITE
ENDFORM.
Output: Local – although the global table field T100-TEXT has “global” contents. (This kind of name assignment of work fields is, of course, not recommended.)
Example
TABLES TRDIR.
DATA: F(8) VALUE ‘F_global’,
G(8) VALUE ‘G_global’.
MOVE ‘XYZ_PROG’ TO TRDIR-NAME.
PERFORM U.
FORM U.
DATA: F(8) VALUE ‘F_local’,
NAME(30) VALUE ‘F’.
FIELD-SYMBOLS
ASSIGN (NAME) TO
WRITE
MOVE ‘G’ TO NAME.
ASSIGN (NAME) TO
WRITE
MOVE ‘TRDIR-NAME’ TO NAME.
ASSIGN (NAME) TO
WRITE
ENDFORM.
Output: F_local G_global XYZ_PROG
Example
PROGRAM P1MAIN.
TABLES TRDIR.
DATA NAME(30) VALUE ‘TFDIR-PNAME’.
FIELD-SYMBOLS
MOVE ‘XYZ_PROG’ TO TRDIR-NAME.
PERFORM U(P1SUB).
ASSIGN (NAME) TO
WRITE
CALL FUNCTION ‘EXAMPLE’.
PROGRAM P1SUB.
TABLES TFDIR.
…
FORM U.
FIELD-SYMBOLS
DATA NAME(30) VALUE ‘TRDIR-NAME’.
ASSIGN TABLE FIELD (NAME) TO
WRITE
MOVE ‘FCT_PROG’ TO TFDIR-PNAME.
ENDFORM.
FUNCTION-POOL FUN1.
FUNCTION EXAMPLE.
DATA NAME(30) VALUE ‘TRDIR-NAME’.
FIELD-SYMBOLS
ASSIGN (NAME) TO
IF SY-SUBRC = 0.
WRITE
ELSE.
WRITE / ‘TRDIR-NAME cannot be accessed’.
ENDIF.
ENDFUNCTION.
Output: XYZ_PROG FCT_PROG
TRDIR-NAME cannot be accessed
Example
TABLES TRDIR.
MOVE ‘XYZ_PROG’ to TRDIR-NAME.
PERFORM U USING TRDIR.
FORM U USING X STRUCTURE TRDIR.
FIELD-SYMBOLS
DATA NAME(30) VALUE ‘X-NAME’.
ASSIGN (NAME) TO
WRITE
ENDFORM.
Output: XYZ_PROG
Variant 4
ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO
Additions:
1. … TYPE typ
2. … DECIMALS dec
Note
This statement is for internal use only. Incompatible changes or further developments may occur at any time without warning or notice.
Effect
Identical to variant 3, except that the system searches for the field whose name is in f steht only in the data in the program group of the main program declared with TABLES. However, the field symbol then points not directly to the found field, but to a copy of this field on theq value stack. This variant therefore ensures that any access to Dictionary fields of an external program group is read only and no changes
are made.
Example
PROGRAM P1MAIN.
TABLES TRDIR.
DATA NAME(30) VALUE ‘TFDIR-PNAME’.
FIELD-SYMBOLS
MOVE ‘XYZ_PROG’ TO TRDIR-NAME.
CALL FUNCTION ‘EXAMPLE’.
FUNCTION-POOL FUN1.
FUNCTION EXAMPLE.
DATA NAME(30) VALUE ‘TRDIR-NAME’.
FIELD-SYMBOLS
ASSIGN LOCAL COPY OF MAIN
TABLE FIELD (NAME) TO
IF SY-SUBRC = 0.
WRITE
ELSE.
WRITE / ‘TRDIR-NAME cannot be accessed’.
ENDIF.
ENDFUNCTION.
Output: XYZ_PROG
Addition 1 … TYPE typ
Addition 2 … DECIMALS dec
Effect
See similar additions to variant 1.
Variant 5 ASSIGN COMPONENT idx OF STRUCTURE rec TO
Variant 6 ASSIGN COMPONENT name OF STRUCTURE rec TO
Additions:
1. … TYPE typ
2. … DECIMALS dec
Effect
If the field name or idx has the type C or if it is a field string with no internal table, it is treated as a component name. Otherwise, it is considered as a component number. The corresponding component of the field string rec is assigned to the field symbol
SY-SUBRC = 0: The assignment was successful.
SY-SUBRC = 4: The field could not be assigned to the field symbol.
Note
If idx has the value 0, the entire field string is assigned to the field symbol.
Example
PROGRAM P1MAIN.
DATA: BEGIN OF REC,
A VALUE ‘a’,
B VALUE ‘b’,
C VALUE ‘c’,
D VALUE ‘d’,
END OF REC,
CN(5) VALUE ‘D’.
FIELD-SYMBOLS
DO 3 TIMES.
ASSIGN COMPONENT SY-INDEX OF
STRUCTURE REC TO
IF SY-SUBRC <> 0. EXIT. ENDIF.
WRITE
ENDDO.
ASSIGN COMPONENT CN OF STRUCTURE REC TO
WRITE
Output: a b c d
Addition 1 … TYPE typ
Addition 2 … DECIMALS dec
Effect
See similar additions to variant 1.
Note
Runtime errors: Depending on the operands, the ASSIGN statement can cause runtime errors.
Note
Performance: For performance reasons, you are recommended to use typed field symbols. The runtime for a typed ASSIGN statement amounts to approx. 9 msn (standardized microseconds) against approx. 13 msn for an untyped ASSIGN statement.
———————
ABAPer, mail: abap.community@gmail.com http://www.erpdb.info
If you like this post, you may as well like these too:
- AT : ABAP Keyword a day AT Events in lists- AT LINE-SELECTION.- AT USER-COMMAND.- AT PFn.Events on selection screens- AT SELECTION-SCREEN.Control break with extracts- AT NEW f.- AT END OF f.- AT FIRST.- AT LAST.- AT...
- GET: ABAP Keyword a day GET: Event keyword for defining event blocks for reporting events. GET <node> [FIELDS <fi> <f 2>...]. GET node LATE [FIELDS f1 f2 ...]. Only occurs in executable programs. When the...
- DO : ABAP Keyword a day DO Variants 1. DO. 2. DO n TIMES. Variant 1 DO. Addition … VARYING f FROM f1 NEXT f2 Effect Repeats the processing enclosed by the DO and ENDDO statements...
- ADD-CORRESPONDING : ABAP Keyword a day ADD-CORRESPONDING Basic form ADD-CORRESPONDING rec1 TO rec2. Effect Interprets rec1 and rec2 as field strings. If, for example, rec1 and rec2 are tables, executes the statement for their header lines....
- FORM: ABAP Keyword a day FORM: Defines a subroutine. Syntax FORM [USING ... [VALUE(][)] [TYPE |LIKE ]„. ] [CHANGING... [VALUE(][)] [TYPE |LIKE ]„. ]. Introduces a subroutine. The USING and CHANGING additions define the subroutine’s...
- FETCH : ABAP Keyword a day FETCH Basic form FETCH NEXT CURSOR c target. Effect Uses the cursor c to read the next line or lines from the dataset of a database table determined by OPEN...
- EXPORT : ABAP Keyword a day EXPORT 1 2 3 4 *Export data - EXPORT obj1 ... objn TO MEMORY. - EXPORT obj1 ... objn TO DATABASE dbtab(ar) ID key. - EXPORT obj1 ... objn TO...
- DIVIDE : ABAP Keyword a day DIVIDE Basic form DIVIDE n1 BY n2. Effect Divides the contents of n1 by n2 and places the result in n1 . This is equivalent to: n1 = n1 /...
- DESCRIBE : ABAP Keyword a day DESCRIBE Return attributes of a field - DESCRIBE FIELD f. Return attributes of an internal table - DESCRIBE TABLE itab. Determine distance between two fields - DESCRIBE DISTANCE BETWEEN f1...
- DATA : ABAP Keyword a day DATA Variants1. DATA f.2. DATA f(len).3. DATA: BEGIN OF rec,…END OF rec.4. DATA: BEGIN OF itab OCCURS n,…END OF itab.5. DATA: BEGIN OF COMMON PART c,…END OF COMMON PART.EffectDefines global...
- CONTINUE: ABAP Keyword a day CONTINUE Basic formCONTINUE.EffectWithin loop structures like * DO … ENDDO* WHILE … ENDWHILE* LOOP … ENDLOOP* SELECT … ENDSELECTCONTINUE terminates the current loop pass, returns the processing to the beginning...
- CONCATENATE : ABAP Keyword a day CONCATENATE Basic form CONCATENATE f1 … fn INTO g. Addition … SEPARATED BY h Effect Places the fields f1 to fn after g . With the fields fi (1 <=...
- COMPUTE: ABAP Keyword a day COMPUTEBasic formCOMPUTE n = arithexp.EffectEvaluates the arithmetic expression arithexp and places the result in the field n .Allows use of the four basic calculation types + , – , *...
- CHECK : ABAP Keyword a day CHECK Within loops and events- CHECK logexp.Special for reports with logical databases- CHECK sel.- CHECK SELECT-OPTIONS. CHECK – within loops Basic formCHECK logexp.EffectCHECK evaluates the subsequent logical expression . If...
- GET PARAMETER: ABAP Keyword a day GET PARAMETER Gets an SPA/GPA parameters. Syntax GET PARAMETER ID <pid> FIELD <field_name>. Writes the value of the SPA/GPA parameter <pid> from the user-specific SAP memory into the variable <f>....



















Leave a Reply