SAP WRITE TO ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID WRITE-TO
• WRITE TO ABAP Statement
• TO WRITE

WRITE, TO
Short Reference

ABAP_SYNTAX
WRITE ${source$|(source_name)$} TO destination
$[format_options$].

What does it do?
The statement WRITE TO prepares the content of a source either using
predefined formats or
explicit formatting options format_options
as a character string and assigns the formatted content to the character-like data object destination.
The source field can be specified either statically or dynamically:
source The static specification is made directly as source. source is a general expression position with the restriction that no numeric literals, arithmetic expressions, or bit expressions can be specified directly.
source_name The dynamic specification is made as the content of a parenthesized flat character-like data object source_name, which must contain the name of the data object in question (non-case-sensitive). If the data object specified in source_name does not exist, no assignment takes place and sy-subrc is set to 4. When evaluating source_name, the same applies as to a dynamically specified (name) in the statement ASSIGN. The following can be specified for source_name:
Literal or constant
If the data object source_name is specified as a text field literal or as a constant, it can be evaluated statically, and the specified data object is recognized as the used object.
Variable
BEGIN_SECTION ID WRITE-TO-DYNAMIC
If the data object source_name is specified as a variable, it is specified only dynamically and the content is not evaluated statically. When the statement is executed, name is not evaluated until runtime in both cases.
BEGIN_SECTION SAP_INTERNAL_HINT
Even the access to data of other programs of the same ABAP_ISESS is possible via (prog)name - also in lower case.
END_SECTION SAP_INTERNAL_HINT
END_SECTION ID WRITE-TO-DYNAMIC
Only the data types grouped together under the simple generic type can be used as source fields.
All flat data types; flat structures are handled like a data object of type c and can only contain character-like components.
The data types string and xstring
enumerated types; the name (maximum three characters) of the enumerated constant that defines the current enumerated value is used in uppercase letters.
The target field destination must be character-like and flat.
If the length of the target field destination is shorter than the length of the format of the data object either predefined or specified using format_options, the output is truncated in formatting and in the subsequent assignment in accordance with a type-specific truncation behavior.
If the available length is greater than the length required for formatting, the system places the result in the target field based on the predefined or user-defined alignment and fills the non-required space with blanks.
System Fields sy-subrcMeaning 0The data object specified in source_name was found and the assignment was performed. 4The data object specified in source_name was not found and the assignment was not performed.
If source is specified statically, sy-subrc is not set.



Latest notes:

Although the statement WRITE TO does not send any data to an output medium, a conversion routine may be executed.
The statement WRITE TO is mainly suited for formatting data for output purposes but not for further internal processing. For example, a field can no longer be handled as a numeric data object if the decimal separator is represented as a comma.
No numeric literals can be specified between WRITE and TO because the statement cannot be distinguished from a WRITE statement with a specified position.
The use of the WRITE TO statement can be replaced in most cases by embedded expressions in string templates of string expressions.
It is possible to use string templates as source fields for the statement WRITE TO, but this is not usually recommended since both WRITE TO and string templates are used to format data and only a single method should be used. It is best to use string templates only.
ABAP_HINT_END



Example ABAP Coding

After the assignment, the variables date_short and date_long contain the current local date in the order specified in the user master record. The variable date_long contains the separators defined there. The variable date_short does not contain any separators because their length is not sufficient. The content of the variable date_mask is formatted according to the formatting addition DD/MM/YY, for which their length is sufficient.
ABEXA 00789
ABAP_EXAMPLE_END



Runtime Exceptions



Catchable Exceptions
If one of the following catchable exceptions is raised in the output of a decimal floating point number, the target field or output of the statement WRITE is filled using asterisks (*).
CX_SY_WRITE_INVALID_STYLE
Reason for error:
Incorrect output format for decimal floating point numbers with the STYLE addition.
Runtime error:
WRITE_INVALID_STYLE
CX_SY_CONVERSION_NO_NUMBER
Reason for error:
Invalid format of the source field in the output of a decimal floating point number.
Runtime error:
CONVT_NO_NUMBER
CX_SY_CONVERSION_OVERFLOW
Reason for error:
Target field is too short to represent a decimal floating point number.
Runtime error:
CONVT_OVERFLOW

Non-catchable Exceptions
Reason for error:
Negative length specified when offset/length is specified.
Runtime error:
WRITE_TO_LENGTH_NEGATIVE
Reason for error:
Negative offset specified when offset/length is specified.
Runtime error:
WRITE_TO_OFFSET_NEGATIVE
Reason for error:
Offset specified when offset/length is specified is greater than the field length.
Runtime error:
WRITE_TO_OFFSET_TOOLARGE
Reason for error:
CURRENCY parameter in WRITE does not have type c
Runtime error:
WRITE_CURRENCY_ILLEGAL_TYPE
Reason for error:
Rounding parameter less than -14
Runtime error:
WRITE_ROUND_TOO_SMALL
Reason for error:
UNIT parameter in WRITE does not have type c
Runtime error:
WRITE_UNIT_ILLEGAL_TYPE
Reason for error:
Incomplete rules for a time zone when using the addition TIME ZONE
Runtime error:
CONVERT_TSTMP_INCONSISTENT_TAB
ABAP_NONCAT_END

Return to menu