100% found this document useful (1 vote)
172 views11 pages

Types Type Table of With KEY Data Type Ref To Create Data NEW NEW

The document describes several new features in ABAP including inline declarations using DATA and FIELD-SYMBOL, constructor operators NEW and VALUE for creating data objects, table expressions for accessing table rows, LET expressions for defining auxiliary variables, MOVE-CORRESPONDING for structures and internal tables, predefined table functions, and table comprehensions for populating internal tables.

Uploaded by

Kishore Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
172 views11 pages

Types Type Table of With KEY Data Type Ref To Create Data NEW NEW

The document describes several new features in ABAP including inline declarations using DATA and FIELD-SYMBOL, constructor operators NEW and VALUE for creating data objects, table expressions for accessing table rows, LET expressions for defining auxiliary variables, MOVE-CORRESPONDING for structures and internal tables, predefined table functions, and table comprehensions for populating internal tables.

Uploaded by

Kishore Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Inline Declarations:

The new declaration operators DATA and FIELD-SYMBOL make inline declarations.

Example1
Before: 7.4 DATA: lv_text TYPE STRING.
lv_text = ‘Vistex’.
After 7.4: Data(lv_text) = ‘Vistex’.

Example 2: Working with Workareas


LOOP AT lt_data INTO DATA(lwa_data)
lwa_data-field1 = 1.
lwa_data-field2 = ‘Asia’.
END OF lt_data.

Constructor Operators:
1. New: This constructor expression with the instance operator NEW creates an anonymous
data object or an instance of a class.

Example:
TYPES ty_itab TYPE TABLE OF t100 WITH EMPTY KEY.
DATA ref_data TYPE REF TO ty_itab.

CREATE DATA ref_data .

ref_data = NEW #( ). # is used when the type is fully recognizable

ref_data = NEW t_itab( ). We can also provide the dtype after NEW

2. Value: This constructor expression with the VALUE operator creates a result of a data type
specified using type.
Syntax: … VALUE type( )…

Example 1:
TYPES:
BEGIN OF ty_data,
kunnr TYPE kunnr,
name1 TYPE name1,
ort01 TYPE ort01,
land1 TYPE land1,
END OF ty_data.
TYPES: tt_data TYPE STANDARD TABLE OF ty_data WITH DEFAULT KEY.

DATA(lt_multi_comp) =
VALUE tt_data( ( kunnr = '123' name1 = 'ABCD' ort01 = 'LV' land1 = 'NV' )
( kunnr = '456' name1 = 'XYZ' ort01 = 'LA' land1 = 'CA' )
).

Example 2:
TYPES: BEGIN OF ty_nest,
key1 TYPE i,
key2 TYPE i,
END OF ty_nest.

TYPES: BEGIN OF ty_struct,


key1 TYPE string,
key2 TYPE ty_nest,
END OF ty_struct.

TYPES: ty_table TYPE STANDARD TABLE OF ty_struct WITH DEFAULT KEY.

*Option1
DATA(lwa_struct1) = VALUE ty_struct( key1 = 'Vistex'
key2-key1 = 1
key2-key2 = 2 ).

*Option2
DATA(lwa_nest) = VALUE ty_nest( key1 = 2
key2 = 3 ).
DATA(lwa_struct2) = VALUE ty_struct( key1 = 'Asia'
key2 = lwa_nest ).

*Option3
DATA(lwa_struct3) = VALUE ty_struct( key1 = 'Pacific'
key2 = VALUE #( key1 = 1
key2 = 2 ) ).

DATA(lt_table) = VALUE ty_table( ( key1 = 'A' key2-key1 = 1 key2-key2 = 2 ) ).


Table Expressions:
A table expression consists of an internal table itab, followed directly by a row(table_line)
specified in a square brackets. A chaining -comp[….]->comp can be appended to this row.
This expression searches for a specified row in the internal table.

If no chaining is specified then entire work area or row is returned.


If a chaining is specified, then the following is possible:
 The structure component selector – can be used to access the component comp in the row.
 Square brackets […..] can be used to chain multiple expressions.

Syntax: itab[ itab_line ][-comp|[ ... ]|->comp] ...

Example1:
DATA: lt_data LIKE SORTED TABLE OF lwa_data WITH UNIQUE KEY field1.
DATA: lv_index TYPE i VALUE '1'.
DATA: lv_temp TYPE string.

lwa_data-field1 = 1.
lwa_data-field2 = 'Vistex'.
APPEND lwa_data to lt_data.

lwa_data-field1 = 2.
lwa_data-field2 = 'Asia'.
APPEND lwa_data to lt_data.

lwa_data = lt_data[ lv_index ]. "READ TABLE lt_data INTO lwa_data IND


EX lv_index. " Returns Entire Workarea

ASSIGN lt_data[ 1 ]-field1 to field-SYMBOL(<lv_data1>).

lv_temp = lt_data[ 1 ]-field2+0(3).

ASSIGN lt_data[ 1 ]-field2+0(3) to field-


symbol(<lv_data2>). " Results in syntax error

ASSIGN lt_data[ 2 ] TO FIELD-SYMBOL(<lv_data>).

ASSIGN lt_data[ field1 = 2 ] TO FIELD-SYMBOL(<lv_data>).

Chaining with Table Expression

DATA(lv_data) = lt_data1[ 1 ]-table1[ 1 ]-field2.


Operand Position in CALL FUNCTION…………………….EXPORTING

Actual parameters specified after call function have now become a general expression position.

CALL FUNCTION 'ZKTEST'


EXPORTING
i_number = 1 + 1
IMPORTING
E_NUMBER = lv_number.

Predefined Functions for Internal Tables:

The table function line_index can be used to identify a row number in an index of an internal
table.

DATA(lv_ind) = line_index( lt_multi_comp[ kunnr = '4561' ] ).

The table function line_exists can be used to check the existence of


table rows.

lv_tabix = line_index( lt_itab[ key1 = 1 ] ).

The above line can be used as a replacement for

READ TABLE lt_itab TRANSPORTING NO FIELDS with KEY key1 = 1.


lv_tabix = sy-tabix.
LET Expressions:
The new LET expressions in the form LET…..IN make it possible to define variables or field-
symbols as auxiliary fields in expressions.

Syntax:
... LET {var1 = rhs1}|{<fs1> = wrexpr1}
{var2 = rhs2}|{<fs2> = wrexpr2} ... IN ...

Example:

TYPES: ty_array TYPE STANDARD TABLE OF i WITH empty key,


BEGIN OF ty_line,
col1 TYPE i,
col2 TYPE i,
col3 TYPE i,
END OF ty_line,
ty_itab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.

CONSTANTS: c_factor TYPE i VALUE 10.

DATA(lt_array) = value ty_array( ( 3 ) ( 5 ) ( 7 ) ( 9 ) ).

DATA(lt_itab) = VALUE ty_itab(


FOR x in lt_array INDEX INTO lv_index
LET off = c_factor * lv_index IN
( col1 = x col2 = x * x col3 = x + off ) ).

Move Corresponding for Structures:

Syntax:

MOVE-CORRESPONDING struc1 TO struc2 [EXPANDING NESTED TABLES].

Example:

TYPES: ty_de TYPE c LENGTH 3,

BEGIN OF ty_temp1,
col1 TYPE ty_de,
col2 TYPE ty_de,
END OF ty_temp1,

BEGIN OF ty_temp2,
col2 TYPE ty_de,
col3 TYPE ty_de,
END OF ty_temp2,
BEGIN OF ty_data1,
col1 TYPE ty_de,
col2 TYPE ty_de,
col3 TYPE STANDARD TABLE OF ty_temp1 WITH EMPTY KEY,
END OF ty_data1,

BEGIN OF ty_data2,
col2 TYPE ty_de,
col3 TYPE STANDARD TABLE OF ty_temp2 WITH EMPTY KEY,
col4 TYPE ty_de,
END OF ty_data2.

DATA: ls_struct1 TYPE ty_data1,


ls_struct2 TYPE ty_data2.

ls_struct1 = VALUE #(
col1 = 'A1'
col2 = 'A2'
col3 = VALUE #( ( col1 = 'A11' col2 = 'A12' )
( col1 = 'A21' col2 = 'A22' ) ) ).

ls_struct2 = VALUE #(
col2 = 'B1'
col3 = VALUE #( ( col2 = 'B11' col3 = 'B12' )
( col2 = 'B21' col3 = 'B22' ) )
col4 = 'B2' ).

MOVE-
CORRESPONDING ls_struct1 to ls_struct2. " Here col4 retains its values.
The col3 is changed entirely

MOVE-
CORRESPONDING ls_struct1 to ls_struct2 EXPANDING NESTED TABLES. " Here col4
retains its values. Since COL3 has col2 as same node only col2 is moved and
col3 is cleared in struct2

Move Corresponding for Internal Tables:

Syntax:
MOVE-CORRESPONDING itab1 TO itab2 [EXPANDING NESTED TABLES] [KEEPING TARGET LINES].

Example:

DATA: lt_tab1 TYPE STANDARD TABLE OF ty_data1 with empty key,


lt_tab2 TYPE STANDARD TABLE OF ty_data2 WITH empty key.

lt_tab1 = VALUE #(
( col1 = 'A11'
col2 = 'A12'
col3 = VALUE #( ( col1 = 'A11' col2 = 'A12' )
( col1 = 'A21' col2 = 'A22' ) ) )
( col1 = 'B11'
col2 = 'B12'
col3 = VALUE #( ( col1 = 'B11' col2 = 'B12' )
( col1 = 'B21' col2 = 'B22' ) ) )

( col1 = 'C11'
col2 = 'C12'
col3 = VALUE #( ( col1 = 'C11' col2 = 'C12' )
( col1 = 'C21' col2 = 'C22' ) ) ) ).

lt_tab2 = VALUE #(
( col2 = 'X11'
col3 = VALUE #( ( col2 = 'X11' col3 = 'X12' )
( col2 = 'X21' col3 = 'X22' ) )
col4 = 'X12' )

( col2 = 'Y11'
col3 = VALUE #( ( col2 = 'Y11' col3 = 'Y12' )
( col2 = 'Y21' col3 = 'Y22' ) )
col4 = 'Y12' )

( col2 = 'Z11'
col3 = VALUE #( ( col2 = 'Z11' col3 = 'Z12' )
( col2 = 'Z21' col3 = 'Z22' ) )
col4 = 'Z12' ) ).

MOVE-
CORRESPONDING lt_tab1 to lt_tab2. " Here Col4 is get cleared and nested
table gets copied fully.

MOVE-
CORRESPONDING lt_tab1 to lt_tab2 keeping target lines. " the new results are
appended to the previous values

MOVE-
CORRESPONDING lt_tab1 to lt_tab2 expanding nested tables. " the nested table
is get checked and moved only the columns matched fields

MOVE-
CORRESPONDING lt_tab1 to lt_tab2 expanding nested tables keeping target lines

Table Comprehensions:
Table comprehensions are an enhancement of the NEW and VALUE operator and are used to create the
content of the internal tables.

Example:
TYPES: BEGIN OF ty_line1,
col1 TYPE i,
col2 TYPE i,
col3 TYPE i,
col4 TYPE i,
END OF ty_line1,

BEGIN OF ty_line2,
col1 TYPE i,
col2 TYPE i,
END OF ty_line2.

DATA: lt_tab1 TYPE STANDARD TABLE OF ty_line1 with empty key with unique sort
ed key key components col1,
lt_tab2 TYPE STANDARD TABLE OF ty_line1 with empty key,
lt_tab3 TYPE STANDARD TABLE OF ty_line1 with empty key,
lt_tab4 TYPE STANDARD TABLE OF ty_line2 with empty key,
lt_tab5 TYPE STANDARD TABLE OF i with empty key.

lt_tab1 = value #(
FOR j = 41 then j - 10 UNtil j < 10
( col1 = j col2 = j + 1 col3 = j + 2 col4 = j + 3 ) ).

lt_tab2 = value #(
FOR lwa_tab2 IN lt_tab1 where ( col1 < 30 )
( lwa_tab2 ) ).

lt_tab3 = value #(
FOR lwa_tab2 IN lt_tab1 INDEX INTO lv_index where ( col1 = 21 )
( lines of lt_tab1 from lv_index ) ).

lt_tab4 = value #(
for lwa_tab2 IN lt_tab1 from 2 to 3
( col1 = lwa_tab2-col2 col2 = lwa_tab2-col3 ) ).

Example 2:
TYPES: BEGIN OF ty_line1,
key TYPE c LENGTH 1,
col1 TYPE i,
col2 TYPE i,
END OF ty_line1,
ty_tab1 TYPE STANDARD TABLE OF ty_line1 with empty key,

BEGIN OF ty_line2,
key TYPE c LENGTH 1,
col1 TYPE i,
col2 TYPE i,
END OF ty_line2,
ty_tab2 TYPE STANDARD TABLE OF ty_line2 with empty key,

BEGIN OF ty_line3,
key TYPE c LENGTH 1,
col11 TYPE i,
col12 TYPE i,
col21 TYPE i,
col22 TYPE i,
END OF ty_line3,
ty_tab3 TYPE STANDARD TABLE OF ty_line3 WITH empty key.

DATA: lt_tab2 TYPE TABLE OF ty_line2 with empty key.

DATA(lt_tab1) = VALUE ty_tab1(


( key = 'a' col1 = 11 col2 = 12 )
( key = 'b' col1 = 21 col2 = 22 )
( key = 'c' col1 = 31 col2 = 32 ) ).

lt_tab2 = value #(
( key = 'a' col1 = 13 col2 = 14 )
( key = 'a' col1 = 23 col2 = 24 )
( key = 'a' col1 = 33 col2 = 34 ) ).

DATA(lt_tab3) = VALUE ty_tab3(


FOR lwa_tab1 IN lt_tab1
( key = lwa_tab1-key
col11 = lwa_tab1-col1
col12 = lwa_tab1-col2
col21 = lt_tab2[ key = lwa_tab1-key ]-col1
col22 = lt_tab2[ key = lwa_tab1-key ]-col2 ) ).

DATA(lt_tab4) = VALUE ty_tab3(


for lwa_tab1 IN lt_tab1 INDEX INTO lv_index
for lwa_tab2 IN lt_tab2 where ( key = lwa_tab1-key )
(
key = lwa_tab1-key
col11 = lwa_tab1-col1
col12 = lwa_tab1-col2
col21 = lwa_tab2-col1
col22 = lwa_tab2-col2 ) ).

Table Filtering:
Filter operator is used to filter the contents of an internal table.

... FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 [...]] ) ...
DATA: lt_tab1 TYPE TABLE OF z205_emphd WITH EMPTY KEY WITH NON-
UNIQUE SORTED KEY emp COMPONENTS empid fname,
lv_empid TYPE /ngv/bptremply VALUE '009',
lv_fname TYPE /ngv/b0fname VALUE 'LEELA PRASAD'.

DATA: BEGIN OF ls_filter,


eid TYPE /ngv/bptremply,
fnm TYPE /ngv/b0fname,
END OF ls_filter,
lt_filter LIKE SORTED TABLE OF ls_filter WITH UNIQUE KEY eid fnm.
SELECT * FROM z205_emphd INTO TABLE lt_tab1.

DATA(lt_extract) = FILTER #( lt_tab1 USING KEY emp


where empid = lv_empid
and fname = lv_fname ).

DATA(lt_rest) = FILTER #( lt_tab1 EXCEPT USING KEY emp


where empid = lv_empid
and fname = lv_fname ).

ls_filter-eid = '009'.
ls_filter-fnm = 'LEELA PRASAD'.
APPEND ls_filter to lt_filter.

DATA(lt_table_filter) = FILTER #( lt_tab1 IN lt_filter


where empid = eid
and fname = fnm ).

Union:
It is used to combine the result from 2 different tables using Union
operator.

SELECT a AS c1, b AS c2, c AS c3, d AS c4


FROM demo_join1
UNION ALL
SELECT d AS c1, e AS c2, f AS c3, g AS c4
FROM demo_join2
UNION ALL
SELECT i AS c1, j AS c2, k AS c3, l AS c4
FROM demo_join3
INTO TABLE @DATA(result_distinct).

Object Component Selector:

DATA itab TYPE TABLE OF REF TO scarr WITH EMPTY KEY.

itab = VALUE #( ( NEW scarr( carrid = 'XX' carrname = 'YYYY' ) ) ).

WRITE: itab[ 1 ]->*.


WRITE / itab[ 1 ]->carrid.

Switch Operator:
DATA: lv_flag TYPE c value 'X'.

DATA(lv_status) = SWITCH #( lv_flag


WHEN 'X' THEN 'Passed'
ELSE 'failed'
).
Meshes:
This is used to join 2 different internal tables.

You might also like