SAP TRUNCATE ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for TRUNCATE

TRUNCATE DATASET

Short Reference
• TRUNCATE DATASET ABAP Statement
• AT TRUNCATE DATASET


ABAP Syntax TRUNCATE DATASET dset AT {CURRENT
POSITION}|{POSITION pos}.

ABAP Addition
1 ... CURRENT POSITION
2 ... POSITION pos

What does it do? This statement sets the end of file of the file specified in dset to the value specified after AT and can thus change the size of the file. When shortened, the file is truncated after the new end of file; when extended, the file is padded with hexadecimal 0 from the previous end of file to the new end of file.

dset expects a character-like data object containing the physical name of the file. The file must be opened for writing, appending, or changing, and not contain the FILTER addition to the OPEN DATASET statement; otherwise a non-handleable exception is raised.

System Fields

The statement always sets sy-subrc to the value 0 or raises an exception.



Latest notes:The TRUNCATE statement does not change the position
of the current file pointer. If the file is open for appending, the file pointer is only set directly before the next write to the end of file.
• CURRENT POSITION TRUNCATE DATASET

ABAP Addition

What does it do? The CURRENT POSITION addition sets the end of file to the current file pointer.
• POSITION TRUNCATE DATASET

ABAP Addition

What does it do? The POSITION pos addition sets the end of file to the position specified in pos. pos expects a numeric data object whose contents cannot be negative. The positioning is specified in bytes, where the start of file is synonymous with the position 0.



Example ABAP Coding
After the first TRUNCATE statement, the file
contains the value 'FF', and after the second, the value 'FF00'. DATA: name TYPE string VALUE `test.dat`,
hex TYPE xstring.

hex = 'FFFF'.

OPEN DATASET name FOR OUTPUT IN BINARY MODE.

TRANSFER hex TO name.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

TRUNCATE DATASET name AT POSITION 1.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

TRUNCATE DATASET name AT POSITION 2.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

CLOSE DATASET name.



Runtime Exceptions

Catchable Exceptions
CX_SY_FILE_OPEN
Reason for error:
The file is not open.
Runtime error:
DATASET_NOT_OPEN
Reason for error:
The file is only open for reading.
Runtime error:
DATASET_READ_ONLY

CX_SY_FILE_AUTHORITY
Reason for error:
No authorization for access to file
Runtime error:
OPEN_DATASET_NO_AUTHORITY

CX_SY_FILE_POSITION
Reason for error:
Invalid position specified.
Runtime error: DATASET_OFFSET_TOO_LARGE

CX_SY_FILE_TRUNCATE
Reason for error:
The operating system could not change the size of the file.
Runtime error:
DATASET_TRUNCATE_ERROR
Reason for error:
An attempt was made to change the size of a file opened using the FILTER addition.
Runtime error:
DATASET_TRUNCATE_ERROR


Non-catchable Exceptions
Reason for error:
Internal error when emptying the file buffer, determining the current file position, or restoring the file when rolling in the internal session.
Runtime error:
DATASET_TRUNCATE_ERROR

Return to menu