""Brought to you by www.anubhavtrainings.
com
CLASS zcl_ats_new_abap_syntax DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
CLASS-METHODS : s1_using_key_expression.
CLASS-METHODS : s1_constructor_expression.
CLASS-METHODS : s1_table_expression.
CLASS-METHODS: s1_inline_declaration.
class-METHODS : s1_value_expression.
class-METHODS : s1_corresponding_data.
class-METHODS : s1_cond_conv_exp.
class-METHODS : s1_loop_with_grouping.
class-METHODS : s1_loop_with_single_line.
class-METHODS : s1_loop_reduce_statement.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_ats_new_abap_syntax IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
zcl_ats_new_abap_syntax=>s1_using_key_expression( ).
zcl_ats_new_abap_syntax=>s1_constructor_expression( ).
zcl_ats_new_abap_syntax=>s1_table_expression( ).
zcl_ats_new_abap_syntax=>s1_loop_reduce_statement( ).
zcl_ats_new_abap_syntax=>s1_loop_with_single_line( ).
zcl_ats_new_abap_syntax=>s1_loop_with_grouping( ).
zcl_ats_new_abap_syntax=>s1_cond_conv_exp( ).
zcl_ats_new_abap_syntax=>s1_inline_declaration( ).
zcl_ats_new_abap_syntax=>s1_value_expression( ).
zcl_ats_new_abap_syntax=>s1_corresponding_data( ).
ENDMETHOD.
METHOD s1_using_key_expression.
data : itab TYPE SORTED TABLE OF /dmo/booking
WITH UNIQUE KEY travel_id
booking_id
WITH NON-UNIQUE SORTED KEY
spiderman COMPONENTS carrier_id connection_id.
select * UP TO 20 rows from /dmo/booking into table @itab.
""Table expression
data(wa) = itab[ key spiderman carrier_id = 'AA' connection_id = 0322 ] .
""Loop at itab
loop at itab into wa USING KEY spiderman WHERE carrier_id = 'AA'.
ENDLOOP.
ENDMETHOD.
METHOD s1_constructor_expression.
data(lo_obj) = new /dmo/cm_flight_messages(
textid = value #( msgid = 'SY' msgno = 499 )
severity = if_abap_behv_message=>severity-error
).
* data: lo_obj type ref to /dmo/cm_flight_messages.
* create object lo_obj
* EXPORTING
* textid = value #( msgid = 'SY' msgno = 499 )
* severity = if_abap_behv_message=>severity-error
* .
ENDMETHOD.
METHOD s1_table_expression.
data : itab type table of /dmo/booking WITH DEFAULT KEY.
select * UP TO 20 rows from /dmo/booking into table itab.
""Read table itab into data(wa) with key travel_id = ''.
if not line_exists( itab[ travel_id = '00000001' ] ).
WRITE : / 'oops the record was not found'.
exit.
ENDIF.
data(wa) = itab[ travel_id = '00000001' carrier_id = 'UA' ].
WRITE : / wa-travel_id, wa-booking_date, wa-booking_id, wa-connection_id,
wa-carrier_id.
data(lv_field) = itab[ travel_id = '00000001' ]-flight_price.
WRITE : lv_field.
ENDMETHOD.
METHOD s1_loop_reduce_statement.
types: tt_bookings type table of /dmo/booking WITH DEFAULT KEY.
data lv_total type p DECIMALS 2.
select * up to 20 rows from /dmo/booking into table @data(lt_bookings).
* loop at lt_bookings into data(ls_bookings).
* lv_total = lv_total + ls_bookings-flight_price.
* ENDLOOP.
lv_total = REDUCE dec10( INIT x = conv dec10( 0 )
for ls_bookings in lt_bookings
NEXT x = x + ls_bookings-flight_price
).
WRITE : / lv_total.
ENDMETHOD.
METHOD s1_loop_with_single_line.
types: tt_bookings type table of /dmo/booking WITH DEFAULT KEY.
data : lv_total type p DECIMALS 2.
types: BEGIN OF ty_final_booking.
INCLUDE type /dmo/booking.
TYPes: booking_tx TYPE p LENGTH 10 DECIMALS 2,
END OF ty_final_booking,
tt_final_booking type table of ty_final_booking WITH DEFAULT KEY.
data: lv_gst type p DECIMALS 2.
lv_gst = '1.12'.
select * up to 20 rows from /dmo/booking into table @data(lt_bookings).
data(lt_final_booking) = value tt_final_booking( FOR wa IN lt_bookings (
travel_id = wa-travel_id
booking_id = wa-booking_id
flight_price = wa-flight_price
booking_tx = cond #( when wa-flight_price > 600
then wa-flight_price * lv_gst
else wa-flight_price
)
) ).
loop at lt_final_booking into data(ls_booking).
WRITE : / ls_booking-travel_id, ls_booking-booking_id, ls_booking-
flight_price,
ls_booking-booking_tx.
ENDLOOP.
* loop at lt_bookings into wa.
* ---IF condition
* ---calculations lv_amt = lv_gst * wa-booking_amt.
* ---move corresponding wa to wa2
* ---wa2-booking_tx = lv_amt
* ---apend wa2 to itab2
* endloop.
ENDMETHOD.
METHOD s1_loop_with_grouping.
types: tt_bookings type table of /dmo/booking WITH DEFAULT KEY.
data : lv_total type p DECIMALS 2.
select * up to 20 rows from /dmo/booking into table @data(lt_bookings).
"grouping data by a key
loop at lt_bookings into data(ls_bookings) GROUP BY ls_bookings-travel_id.
WRITE : / 'Travel Request' , ls_bookings-travel_id.
WRITE : / 'Bookings :' .
data(lt_grp_book) = value tt_bookings( ).
""loop at all the child of that group
loop at GROUP ls_bookings into data(ls_child_rec).
"append lines of itab1 to itab2.
lt_grp_book = value #( BASE lt_grp_book ( ls_child_rec ) ).
WRITE : /(5) ls_child_rec-booking_id, ls_child_rec-carrier_id,
ls_child_rec-flight_price.
lv_total = lv_total + ls_child_rec-flight_price.
ENDLOOP.
WRITE : / 'Total Value of the Bookings :' , lv_total.
clear : lv_total.
ENDLOOP.
ENDMETHOD.
METHOD s1_cond_conv_exp.
data : lv_numc type numc04 value '0600',
lv_num type i,
lv_res type c.
""used for type casting for matching data types
lv_num = conv #( lv_numc ).
"to check a simple condition - inline with program code
lv_res = cond #( let val = 800 in
when lv_num > val then 'X'
else '' ) .
WRITE : / 'result is ', lv_res.
ENDMETHOD.
METHOD s1_corresponding_data.
types: BEGIN OF ty_game,
captain type c LENGTH 10,
team type c LENGTH 10,
score type i,
end of ty_game,
tt_game type table of ty_game WITH DEFAULT KEY,
BEGIN OF ty_game2,
scrumlead type c LENGTH 10,
scrum type c LENGTH 10,
goals type i,
end of ty_game2,
tt_game2 type table of ty_game2 WITH DEFAULT KEY.
data : lt_game2 type tt_game2.
data(lt_game) = value tt_game( ( captain = 'Dhoni'
team = 'CSK'
score = 100 )
( captain = 'Virat'
team = 'RCB'
score = 90 )
( captain = 'Dravid'
team = 'MIM'
score = 120 ) ).
*MOVE-CORRESPONDING source TO target.
* lt_game2 = CORRESPONDING #( lt_game EXCEPT score ).
lt_game2 = CORRESPONDING #( lt_game MAPPING
scrumlead = captain
scrum = team
goals = score
).
* APPEND CORRESPONDING #( str ) to itab2.
loop at lt_game2 into data(ls_game).
WRITE : / ls_game-scrumlead, ls_game-scrum, ls_game-goals.
ENDLOOP.
ENDMETHOD.
METHOD s1_value_expression.
types: BEGIN OF ty_game,
captain type c LENGTH 10,
team type c LENGTH 10,
score type i,
end of ty_game,
tt_game type table of ty_game WITH DEFAULT KEY.
* data: lt_game type tt_game,
* ls_game type ty_game.
data(lt_game) = value tt_game( ( captain = 'Dhoni'
team = 'CSK'
score = 100 )
( captain = 'Virat'
team = 'RCB'
score = 90 )
( captain = 'Dravid'
team = 'MIM'
score = 120 ) ).
* ls_game-captain = 'Dhoni'.
* ls_game-team = 'CSK'.
* ls_game-score = 100.
* append ls_game to lt_game.
*
* ls_game-captain = 'Virat'.
* ls_game-team = 'RCB'.
* ls_game-score = 80.
* append ls_game to lt_game.
*
* ls_game-captain = 'Dravid'.
* ls_game-team = 'MUM'.
* ls_game-score = 120.
* append ls_game to lt_game.
loop at lt_game into data(ls_game).
WRITE : / ls_game-captain, ls_game-team, ls_game-score.
ENDLOOP.
ENDMETHOD.
METHOD s1_inline_declaration.
"-----Old Approach
* data: lt_mara type table of mara,
* ls_mara type mara.
*
* select * from mara into table lt_mara.
*
* loop at lt_mara into ls_mara.
* WRITE : / ls_mara-matnr, ls_mara-matkl.
* ENDLOOP.
"-----New Approach
select matnr, matkl from mara into table @data(lt_mara).
* loop at lt_mara into data(ls_mara).
* WRITE : / ls_mara-matnr, ls_mara-matkl.
* ENDLOOP.
loop at lt_mara ASSIGNING FIELD-SYMBOL(<fs>).
WRITE : / <fs>-matnr, <fs>-matkl.
ENDLOOP.
cl_uuid_factory=>create_system_uuid( )->create_uuid_c32(
RECEIVING
uuid = data(lv_uuid)
).
WRITE : / lv_uuid.
ENDMETHOD.
ENDCLASS.