SAP TRANSFER ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID TRANSFER
• TRANSFER ABAP Statement
• TO TRANSFER

TRANSFER
Short Reference

ABAP_SYNTAX
TRANSFER dobj TO dset $[LENGTH len$]
$[NO END OF LINE$].

ABAP Addition
1 ... LENGTH len
2 ... NO END OF LINE

What does it do?
This statement passes the content of data object dobj to the file specified in dset. For dobj, data objects with elementary data types and flat structures can be specified. The file must be open for writing, appending, or changing. If a closed or invalid file is accessed, a catchable exception is raised. If the file was opened as a text file, dobj must be character-like. This restriction does not apply to legacy text files.
dset expects a character-like data object containing the physical name of the file. The content is written to the file from the current file pointer. After the data has been passed, the file pointer is positioned after the inserted data. The addition LENGTH can be used to restrict the number of characters or bytes passed.
Effect of the Access Type
The access type defined in the statement OPEN DATASET has the following effect on the data passed from:
A file opened to be read using FOR INPUT cannot be written.
In a file opened for writing using FOR OUTPUT, the system writes to the file from the current file pointer. If the file pointer is positioned after the current start of the file, the file is padded with hexadecimal 0 from the start of the file to the file pointer.
In a file opened for appending using FOR APPENDING, the system writes to the file from the current file pointer, which is always at the end of the file.
In a file opened for changing using FOR UPDATE, the system writes to the file from the current file pointer. If the file pointer is positioned after the end of the file, the file is padded with hexadecimal 0 between the end of the file and the file pointer position.
Effect of the Storage Type
The data is passed regardless of the storage type used to open the file with the statement OPEN DATASET. If the specified storage type requires conversion, it is carried out before writing.
If the file was opened as a text file or a legacy text file, the trailing blank characters are deleted for all data objects, except for those of data type string. The end-of-line marker defined when the file was opened is then added to the remaining content of the data object or to the result of the conversion, and the final result is written byte-by-byte to the file. The appending of the end-of-line marker can be prevented using NO END OF LINE.
If the file was opened as a binary file or a legacy binary file, the content of the data object or the result of the conversion is written byte-by-byte to the file.
ABAP_SUBRC_GENERAL
The statement TRANSFER always sets sy-subrc to the value 0 or raises an exception.



Latest notes:

Only character-like data objects can be written to text files. Only byte-like data objects should be written to binary files. To save numeric data objects or mixed structures, it is best to assign them to character-like or byte-like typed field symbols using the CASTING addition of the statement ASSIGN and save these field symbols.
If parts of a file are to be overwritten, it must be opened for changes.
Enumerated objects are written in accordance with their basic type.
ABAP_HINT_END



Example ABAP Coding

The binary data from the database table SPFLI is passed to a binary file flights.dat. The structure of the table rows passed contains both character-like and numeric fields. Since the type-compliant storage of mixed structures in files is not possible, the binary content of the structure is directly accessed using a typed field symbol < hex_container>. To achieve the same result, the structure wa could be passed directly. The recommended procedure however is to use the field symbol, because it explicitly passes a binary data type to a binary file. This type of storage is only recommended for short-term storage within the same system, because the byte-like content depends on the byte order and the current system code page. For long-term storage or for exchanging between systems, the data should be converted to character-like containers and stored as a text file.
ABEXA 00706
ABAP_EXAMPLE_END
• LENGTH TRANSFER

ABAP Addition

What does it do?
This addition determines how many characters or bytes of the data object dobj are written to the file. len expects a data object of the type i. It contains the number of characters or bytes. In text files, the content of len specifies the number of characters that are written from the memory. For binary files, legacy text files, and legacy binary files, len specifies the number of bytes that are written to the file. The first len characters or bytes are passed and alignment gaps are included in the structures. If the addition LENGTH is not specified, all characters or bytes are passed.
If the value of len is less than or equal to 0, no characters or bytes are passed. If the file is opened as a (legacy) text file, however, an end-of-line marker is inserted into the file by default. If the value of len is greater than the number of characters or bytes in dobj, blank characters or hexadecimal 0 are passed to the file instead of the missing characters or bytes, depending on whether the file was opened as a (legacy) text file or a (legacy) binary file.



Example ABAP Coding

The statement TRANSFER writes the first four characters of a string to a text file.
ABEXA 00707
ABAP_EXAMPLE_END
• NO END OF LINE TRANSFER

ABAP Addition

What does it do?
This addition has the effect that, in text files or legacy text files, no end-of-line marker is appended to the data passed.



Example ABAP Coding

Opens a text file for reads and writes. No end-of-line marker is appended if writes are performed using the TRANSFER statements. In the case of reads, the entire content of the file is placed in the text string. Compare the example with the addition TEXT MODE of the statement OPEN DATASET.
ABEXA 00708
ABAP_EXAMPLE_END



Runtime Exceptions



Catchable Exceptions
CX_SY_CODEPAGE_CONVERTER_INIT
Reason for error:
The required conversion is not supported. (For example, because a language not supported by the conversion was specified using SET LOCALE LANGUAGE.)
Runtime error:
CONVT_CODEPAGE_INIT
CX_SY_CONVERSION_CODEPAGE
Reason for error:
Conversion is not possible.
Runtime error:
CONVT_CODEPAGE
CX_SY_FILE_AUTHORITY
Reason for error:
No authorization to access the file.
Runtime error:
OPEN_DATASET_NO_AUTHORITY
CX_SY_FILE_IO
Reason for error:
Error when writing to the file.
Runtime error:
DATASET_WRITE_ERROR
CX_SY_FILE_OPEN
Reason for error:
File cannot be opened.
Runtime error:
DATASET_CANT_OPEN
CX_SY_FILE_OPEN_MODE
Reason for error:
The file is not open.
Runtime error:
DATASET_NOT_OPEN
Reason for error:
The file was opened using OPEN DATASET ... FOR INPUT. Therefore, it cannot be written.
Runtime error:
DATASET_READ_ONLY
CX_SY_PIPE_REOPEN
Reason for error:
The file was opened using the addition FILTER and since then a switch of the work process took place.
Runtime error:
DATASET_PIPE_CLOSED
CX_SY_TOO_MANY_FILES
Reason for error:
Maximum number of open files exceeded.
Runtime error:
DATASET_TOO_MANY_FILES

Return to menu