ABAP Keyword Documentation → ABAP − Reference → Creating Objects and Values → NEW - Instance Operator → NEW - Internal Tables
NEW - line_spec
Other versions:
7.31 | 7.40 | 7.54
Syntax
... line
| {LINES OF jtab [FROM idx1] [TO idx2] [USING KEY keyname]} ...
Alternatives
1. ... line
2. ... LINES OF jtab [FROM idx1] [TO idx2] [USING KEY keyname]
Effect
Specifies one or more rows to be inserted when constructing an internal table with the instance operator NEW
.
Alternative 1
... line
Effect
Specifies a row. line
can be specified in exactly the same way as in the parentheses of an expression NEW line_type( ...
). Here, line_type
is the row type of the internal table and a corresponding row is constructed. The following special features apply here:
- If a data object is specified for
line
, this object must be compatible with the row type.
- If an expression (built-in function, functional method, calculation expression, constructor expression,
or table expression) is specified for
line
, the result of the expression must be convertible to the row type.
- The syntax does not permit further parentheses for constructing tabular row types to be nested directly in
line
.
Short Form for Structured Row Types
If the row type of the internal table is a structured type, the following short form can be used:
[BASE itab]
col1 = dobj11 ... ( col2 = dobj12 col3 = dobj13 ... )
( col2 = dobj22 col3 = dobj23 ... )
...
col1 = dobj31 col2 = dobj32 ... ( col3 = dobj33 ... )
( col3 = dobj43 ... )
... ).
This has the same semantics as the following form:
[BASE itab]
( col1 = dobj11 ... col2 = dobj12 col3 = dobj13 ... )
( col1 = dobj11 ... col2 = dobj22 col3 = dobj23 ... )
...
( col1 = dobj31 col2 = dobj32 ... col3 = dobj33 ... )
( col1 = dobj31 col2 = dobj32 ... col3 = dobj43 ... )
... ).
Values can be assigned to individual structure components outside of the inner parentheses. An assignment of this type applies to all following inner parentheses until the next assignment is made to the component in question. Assignments outside of the inner parentheses must be followed by at least one inner parenthesis. A component cannot be assigned a value more than once in the construction of a structure, which means that a component assigned a value outside of the inner parentheses can no longer be specified in an inner parenthesis. A component can be specified again outside the inner parentheses and any components previously specified in an inner parenthesis can also be listed outside the parenthesis.
Notes
- The restriction that the content of tabular row types cannot be constructed directly in
line
applies for reasons of legibility (for humans and for machines) and is not a functional restriction. The expression VALUE line_type( ... ) can be specified forline
to construct the content of a tabular row, whereline_type
is the tabular row type.
- Specifying CONV line_type( ... ) for the inserted values is also a way of meeting the compatibility requirements made by the row type.
- The short form for structured row types enables columns of internal tables that are to be given an identical value in blocks to be filled more easily.
Example
Constructs an anonymous internal table with a structured row type (with a short form for the first column):
TYPES:
BEGIN OF t_line,
col1 TYPE i,
col2 TYPE i,
END OF t_line,
t_itab TYPE TABLE OF t_line WITH EMPTY KEY.
DATA(dref) = NEW t_itab( col1 = 1 ( col2 = 11 )
( col2 = 12 )
( col2 = 13 )
col1 = 2 ( col2 = 21 )
( col2 = 22 )
( col2 = 23 ) ).
cl_demo_output=>display( dref->* ).
Alternative 2
... LINES OF jtab [FROM idx1] [TO idx2] [USING KEY keyname]
Effect
Specifies multiple rows. The rows are taken from the internal table jtab
and inserted into the target table as a block. The same applies to jtab
and
the additions FROM
, TO
, and USING
KEY as to the addition LINES
OF of the statement INSERT
and the block is inserted in accordance with these rules. jtab
is a
functional operand position.
Notes
- The rows from
jtab
are inserted into the target table using the table key only, which means thatjtab
must be compatible with the target type.
- If there is a conflict with an existing unique table key, a non-handleable exception is always raised
when inserting multiple rows from an internal table using the statement
INSERT
.
- When standard tables are created, the rows of the table
jtab
are appended to the target table in the same way as with the statementAPPEND LINES OF
.
Example
Constructs an internal table itab
containing the first three rows and last three rows of the previously constructed table alpha
.
DATA:
alpha TYPE TABLE OF string WITH EMPTY KEY,
itab LIKE REF TO alpha.
alpha = VALUE #( FOR j = 0 UNTIL j >= strlen( sy-abcde )
( substring( val = sy-abcde
off = j
len = 1 ) ) ).
itab = NEW #( ( LINES OF alpha FROM 1 to 3 )
( `-` )
( LINES OF alpha FROM lines( alpha ) - 2
TO lines( alpha ) ) ).
cl_demo_output=>display( itab->* ).