-
Notifications
You must be signed in to change notification settings - Fork 286
/
zcl_demo_abap_structures.clas.abap
1486 lines (1133 loc) · 54 KB
/
zcl_demo_abap_structures.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"! <p class="shorttext"><strong>Structures</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates various syntax options for working with structures.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Note</h2>
"! <p>Find information on <strong>getting started with the example class</strong> and the
"! <strong>disclaimer</strong> in the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</p>
CLASS zcl_demo_abap_structures DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_oo_adt_classrun.
CLASS-METHODS: class_constructor.
PROTECTED SECTION.
PRIVATE SECTION.
"Creating structured data types
TYPES: "Flat structure
BEGIN OF gty_struc,
num1 TYPE i,
num2 TYPE i,
char1 TYPE c LENGTH 10,
char2 TYPE c LENGTH 5,
pnum TYPE p LENGTH 8 DECIMALS 2,
END OF gty_struc,
"Structures within deep structure
BEGIN OF line1,
col1 TYPE i,
col2 TYPE i,
END OF line1,
BEGIN OF line2,
col2 TYPE i,
col3 TYPE i,
col4 TYPE i,
END OF line2.
CLASS-DATA:
"Flat structure
gs_struc TYPE gty_struc,
"Deep structure 1
BEGIN OF gs_deep1,
comp1 TYPE c LENGTH 1 VALUE 'W',
BEGIN OF substruc,
comp1 TYPE c LENGTH 1 VALUE 'X',
BEGIN OF comp2,
col1 TYPE c LENGTH 1 VALUE 'Y',
col2 TYPE c LENGTH 1 VALUE 'Z',
END OF comp2,
END OF substruc,
itab TYPE TABLE OF line1 WITH EMPTY KEY,
END OF gs_deep1,
"Deep structure 2
BEGIN OF gs_deep2,
BEGIN OF substruc,
comp1 TYPE string,
comp2 TYPE string,
comp3 TYPE string,
END OF substruc,
itab TYPE TABLE OF line2 WITH EMPTY KEY,
comp4 TYPE i,
END OF gs_deep2,
"Creating internal table for displaying purposes
gt_tab TYPE STANDARD TABLE OF zdemo_abap_tab1
WITH NON-UNIQUE KEY key_field.
CLASS-METHODS:
initialize_dbtabs,
fill_deep_structures,
select_from_dbtab.
ENDCLASS.
CLASS zcl_demo_abap_structures IMPLEMENTATION.
METHOD class_constructor.
initialize_dbtabs( ).
fill_deep_structures( ).
"Filling demo database tables.
zcl_demo_abap_aux=>fill_dbtabs( ).
ENDMETHOD.
METHOD fill_deep_structures.
"Clearing all content of gs_deep2
CLEAR gs_deep2.
"Filling nested tables in deep structures
gs_deep2-substruc = VALUE #( comp1 = `aaa`
comp2 = `bbb`
comp3 = `ccc` ).
gs_deep1-itab = VALUE #(
( col1 = 111 col2 = 222 )
( col1 = 333 col2 = 444
) ).
gs_deep2-itab = VALUE #(
( col2 = 1 col3 = 2 col4 = 3 )
( col2 = 4 col3 = 5 col4 = 6 )
( col2 = 7 col3 = 8 col4 = 9 )
).
"Filling individual component that is not shared by both structures
gs_deep2-comp4 = 999.
ENDMETHOD.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP cheat sheet example: Structures\n\n| ).
**********************************************************************
out->write( |1) Globally available structures and structured types\n| ).
"Creating structures based on globally available structured types
"Database table
DATA struc_from_dbtab TYPE zdemo_abap_fli.
"CDS view entity
DATA struc_from_cds_ve TYPE zdemo_abap_fli.
"CDS abstract entity
DATA struc_from_cds_abs TYPE zdemo_abap_abstract_ent.
"CDS table function
DATA struc_from_cds_tab_func TYPE zdemo_abap_table_function.
"Globally available structured type in the public visibility section of
"classes/interfaces
DATA struc_from_struc_type_in_cl TYPE zcl_demo_abap_amdp=>fli_struc.
"Creating structured types based on globally available structured types
TYPES ty_struc_from_dbtab TYPE zdemo_abap_fli.
TYPES ty_struc_from_cds_ve TYPE zdemo_abap_fli.
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Nested structure` ) ).
"The following declarations are just included for demonstration purposes
"to show how declarations of local structures and structured
"types can look like.
"Declaring structured type locally (flat structure)
TYPES: BEGIN OF lty_struc,
num1 TYPE i,
num2 TYPE i,
char1 TYPE c LENGTH 10,
char2 TYPE c LENGTH 5,
pnum TYPE p LENGTH 8 DECIMALS 2,
END OF lty_struc.
"Alternatively, you could use the following syntax.
"However, a chained statement as above provides better readability.
TYPES BEGIN OF gs_struc_alt.
TYPES num1 TYPE i.
TYPES num2 TYPE i.
TYPES char1 TYPE c LENGTH 10.
TYPES char2 TYPE c LENGTH 5.
TYPES pnum TYPE p LENGTH 8 DECIMALS 2.
TYPES END OF gs_struc_alt.
"Creating local structures
"a. Based on a local structured type.
DATA ls_struc TYPE lty_struc.
"b. Based on global types in the DDIC
DATA ls_glo_tab TYPE zdemo_abap_flsch. "database table
"c. Directly declaring a structure with DATA and specifying the components
DATA: BEGIN OF ls_direct_decl,
num1 TYPE i,
num2 TYPE i,
char1 TYPE c LENGTH 10,
char2 TYPE c LENGTH 5,
pnum TYPE p LENGTH 8 DECIMALS 2,
END OF ls_direct_decl.
"d. Alternatively, you could use the following syntax.
"However, a chained statement as above provides better readability.
DATA BEGIN OF ls_direct_decl_alt.
DATA num1 TYPE i.
DATA num2 TYPE i.
DATA char1 TYPE c LENGTH 10.
DATA char2 TYPE c LENGTH 5.
DATA pnum TYPE p LENGTH 8 DECIMALS 2.
DATA END OF ls_direct_decl_alt.
"e. Based on structure and internal table (type)
DATA ls_like_dobj LIKE ls_struc.
DATA ls_like_line_of_itab LIKE LINE OF gt_tab.
DATA ls_type_line_of_itab TYPE LINE OF string_table.
"f. Using inline declaration.
"Type is inferred from the right-hand structure; the content is also assigned
DATA(struc_inl1) = ls_struc.
"Declaring structure inline and populating it using the VALUE operator
DATA(struc_inl2) = VALUE lty_struc( num1 = 1 num2 = 2 ).
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Variants of structures` ) ).
out->write( |3) Flat structure with default values\n\n| ).
"Flat structures only contain elementary data types
"Flat structure with default values
DATA: BEGIN OF ls_flat,
num1 TYPE i VALUE 1,
num2 TYPE i VALUE 2,
char1 TYPE c LENGTH 10 VALUE 'abcdefghij',
char2 TYPE c LENGTH 5 VALUE 'klmno',
pnum TYPE p LENGTH 8 DECIMALS 2 VALUE '123.45',
END OF ls_flat.
out->write( data = ls_flat name = `ls_flat` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) Nested structure` ) ).
"Nested structures contain at least one structure as component
"Nested structure with default values
DATA: BEGIN OF ls_nested_address,
BEGIN OF name,
title TYPE string VALUE `Mr.`,
first_name TYPE string VALUE `Duncan`,
surname TYPE string VALUE `Pea`,
END OF name,
BEGIN OF street,
name TYPE string VALUE `Vegetable Lane`,
number TYPE string VALUE `11`,
END OF street,
BEGIN OF city,
zipcode TYPE string VALUE `349875`,
name TYPE string VALUE `Botanica`,
END OF city,
END OF ls_nested_address.
out->write( data = ls_nested_address name = `ls_nested_address` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) Deep structure with strings` ) ).
"Deep structures contain at least one deep component, for
"example, internal tables, strings.
"Deep structure with strings and with default values.
DATA: BEGIN OF ls_flat_address,
name TYPE string VALUE `Mr. Duncan Pea`,
street TYPE string VALUE `Vegetable Lane 11`,
city TYPE string VALUE `349875 Botanica`,
END OF ls_flat_address.
out->write( data = ls_flat_address name = `ls_flat_address` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6) Deep structure with internal table as component` ) ).
"Structured type for nested internal table
TYPES: BEGIN OF lty_flights,
connid TYPE zdemo_abap_flsch-connid,
countryfr TYPE zdemo_abap_flsch-countryfr,
cityfrom TYPE zdemo_abap_flsch-cityfrom,
airpfrom TYPE zdemo_abap_flsch-airpfrom,
countryto TYPE zdemo_abap_flsch-countryto,
cityto TYPE zdemo_abap_flsch-cityto,
airpto TYPE zdemo_abap_flsch-airpto,
END OF lty_flights.
"Creating deep structure
DATA: BEGIN OF ls_flights,
carrier TYPE zdemo_abap_flsch-carrid VALUE 'LH',
carrier_name TYPE zdemo_abap_carr-carrname VALUE 'Lufthansa',
lt_flights TYPE TABLE OF lty_flights WITH EMPTY KEY,
END OF ls_flights.
"Filling nested internal table for the output
SELECT *
FROM zdemo_abap_flsch
WHERE carrid = 'LH'
INTO CORRESPONDING FIELDS OF TABLE @ls_flights-lt_flights
UP TO 4 ROWS.
out->write( data = ls_flights name = `ls_flights` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Accessing and populating structures` ) ).
out->write( |7) Populating structure components using the component selector\n\n| ).
gs_struc-num1 = 1.
gs_struc-num2 = 2.
gs_struc-char1 = 'aaa'.
gs_struc-char2 = 'bbb'.
gs_struc-pnum = '333.33'.
out->write( data = gs_struc name = `gs_struc` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) Populating structure components ` &&
`using the VALUE operator` ) ).
"Value assignments by addressing the structure components individually
"can be very bulky. Hence, the use of the VALUE operator is
"very handy for the value assignment, especially for filling structure
"components at operand position. In below examples the # sign is used
"before the parentheses which means that the type of the operand can be
"implicitly derived.
"Flat structure
gs_struc = VALUE #( num1 = 3
num2 = 4
char1 = 'ccc'
char2 = 'ddd'
pnum = '555.55' ).
"Nested structure
ls_nested_address = VALUE #(
name = VALUE #( title = `Mrs.`
first_name = `Jane`
surname = `Doe` )
street = VALUE #( name = `Main Street`
number = 1 )
city = VALUE #( zipcode = 12345
name = `London` ) ).
"Deep structure
ls_flights = VALUE #(
carrier = 'AA'
carrier_name = 'American Airlines'
lt_flights = VALUE #( ( connid = 17 countryfr = 'US'
cityfrom = 'New York'
airpfrom = 'JFK'
countryto = 'US'
cityto = 'San Francisco'
airpto = 'SFO' )
( connid = 64
countryfr = 'US'
cityfrom = 'San Francisco'
airpfrom = 'SFO'
countryto = 'US'
cityto = 'New York'
airpto = 'JFK' ) ) ).
out->write( data = gs_struc name = `gs_struc` ).
out->write( |\n| ).
out->write( data = ls_nested_address name = `ls_nested_address` ).
out->write( |\n| ).
out->write( data = ls_flights name = `ls_flights` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) Creating and populating a new structure ` &&
`using the VALUE operator` ) ).
"In the example below in which a new structure is created by declaring
"a variable inline the '#' sign cannot be used before the parentheses
"because a type cannot be derived. Instead, the type must be
"specified before the parentheses explicitly.
DATA(ls_copy) = VALUE gty_struc( num1 = 5
num2 = 6
char1 = 'ggg'
char2 = 'hhh'
pnum = '555.55' ).
out->write( data = ls_copy name = `ls_copy` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) Accessing individual components using the ` &&
`component selector` ) ).
"Assigning value of individual component to a variable
DATA(lv_copy) = gs_struc-num1.
"Assigning a value to a component in a nested structure.
ls_nested_address-name-first_name = 'Emma'.
"Assigning a value to a component in a deep structure.
"The table line is determined using a table expression.
ls_flights-lt_flights[ 1 ]-cityto = 'San Fran'.
out->write( data = lv_copy name = `lv_copy` ).
out->write( |\n| ).
out->write( data = ls_nested_address-name-first_name name = `ls_nested_address-name-first_name` ).
out->write( |\n| ).
out->write( data = ls_flights-lt_flights[ 1 ]-cityto name = `ls_flights-lt_flights[ 1 ]-cityto` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `11) Excursion: Addressing components of a variable` &&
` referring to a structure ` ) ).
"Creating a data reference variable.
DATA(ref) = NEW gty_struc( ).
"Assigning a structure to the data reference
ref->* = gs_struc.
"Accessing a component using the object component selector
DATA(ref_comp1) = ref->char1.
"The following syntax is also possible but less comfortable.
DATA(ref_comp2) = ref->*-char2.
out->write( data = ref_comp1 name = `ref_comp1` ).
out->write( |\n| ).
out->write( data = ref_comp2 name = `ref_comp2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `12) Using structure components for ` &&
`data type and data object declarations` ) ).
TYPES: lty_1 TYPE gty_struc-num1,
lty_2 LIKE gs_struc-num2.
DATA: lv_num1 TYPE gty_struc-num1 VALUE 123,
lv_num2 LIKE gs_struc-num2 VALUE 456.
out->write( data = lv_num1 name = `lv_num1` ).
out->write( |\n| ).
out->write( data = lv_num2 name = `lv_num2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `13) Creating and populating an anonymous structure ` &&
`using the NEW operator` ) ).
"In the example below in which a new structure is created by declaring
"a variable inline the '#' sign cannot be used before the parentheses
"because a type cannot be derived. Instead, the type must be
"specified before the parentheses explicitly.
"Creating a data reference variable
TYPES struc_type LIKE ls_flat_address.
DATA addr_ref1 TYPE REF TO struc_type.
"Populating the anonymous structure
addr_ref1 = NEW #( name = `Mr. Duncan Pea`
street = `Vegetable Lane 11`
city = `349875 Botanica` ).
addr_ref1->name = `Mrs. Jane Doe`.
"Declaring an anonymous structure/a data reference variable inline
DATA(addr_ref2) = NEW struc_type( name = `Mr. Duncan Pea`
street = `Vegetable Lane 11`
city = `349875 Botanica` ).
out->write( data = addr_ref2->* name = `addr_ref2->*` ).
addr_ref2->* = VALUE #( BASE addr_ref2->* name = `Mr. John Doe` ).
out->write( data = addr_ref2->* name = `addr_ref2->*` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `14) Copying content of a structure to another ` &&
` that has the same type using the assignment operator` ) ).
"Note: In the case below, a MOVE-CORRESPONDING statement as shown
"further down would have the same effect:
"MOVE-CORRESPONDING gs_struc TO gs_struc_2.
DATA gs_struc_2 TYPE gty_struc.
gs_struc_2 = gs_struc.
out->write( data = gs_struc_2 name = `gs_struc_2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `15) Copying content of a structure to another` &&
` that has an incompatible type using` &&
` MOVE-CORRESPONDING statemtns and the CORRESPONDING operator` ) ).
"Both statements with MOVE-CORRESPONDING and the CORRESPONDING
"operator are used to assign identically named components of
"structures to each other.
"Note: For value assignments, generally bear in mind that there are
"special conversion and comparison rules that apply to assignments.
"The following examples focus on flat structures.
"Creating flat structure with different type and assigning
"default values.
DATA: BEGIN OF gs_struc_diff,
num1 TYPE i VALUE 111,
num2 TYPE i VALUE 222,
char1 TYPE c LENGTH 10 VALUE 'AAA',
c1 TYPE c LENGTH 1 VALUE 'B',
END OF gs_struc_diff.
"Copying structure to have the same values for another syntax variant.
DATA(gs_struc_diff2) = gs_struc_diff.
DATA(gs_struc_diff3) = gs_struc_diff.
DATA(gs_struc_diff4) = gs_struc_diff.
DATA(gs_struc_diff5) = gs_struc_diff.
out->write( |Original content of structures:\n\n| ).
out->write( data = gs_struc name = `gs_struc` ).
out->write( |\n| ).
out->write( data = gs_struc_diff name = `gs_struc_diff` ).
out->write( |\n| ).
"Identically named components are moved...
"... and the content in nonidentical components of the target
"structure are kept.
MOVE-CORRESPONDING gs_struc TO gs_struc_diff.
"... and the content in nonidentical components in the target
"structure are initialized.
gs_struc_diff2 = CORRESPONDING #( gs_struc ).
"... and the content in nonidentical components of the target
"structure are kept. Same as MOVE-CORRESPONDING without additions.
gs_struc_diff3 = CORRESPONDING #( BASE ( gs_struc_diff3 )
gs_struc ).
"MAPPING addition: Specifying components of a source structure that
"are assigned to the components of a target structure in mapping
"relationships. Note the conversion and assignement rules.
gs_struc_diff4 = CORRESPONDING #( BASE ( gs_struc_diff4 )
gs_struc MAPPING c1 = char2 ).
"EXCEPT addition: Excluding components from the assignment.
gs_struc_diff5 = CORRESPONDING #( BASE ( gs_struc_diff5 )
gs_struc EXCEPT num2 ).
out->write( |Results of statements:\n\n| ).
out->write( data = gs_struc_diff name = `gs_struc_diff` ).
out->write( |\n| ).
out->write( data = gs_struc_diff2 name = `gs_struc_diff2` ).
out->write( |\n| ).
out->write( data = gs_struc_diff3 name = `gs_struc_diff3` ).
out->write( |\n| ).
out->write( data = gs_struc_diff4 name = `gs_struc_diff4` ).
out->write( |\n| ).
out->write( data = gs_struc_diff5 name = `gs_struc_diff5` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `16) Copying content of a deep ` &&
`structure to another` ) ).
out->write( |Original content of deep structures:\n\n| ).
"Note: The example purposely uses non-fitting components
"to emphasize conversion and assignment rules.
out->write( data = gs_deep1 name = `gs_deep1` ).
out->write( |\n| ).
out->write( data = gs_deep2 name = `gs_deep2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `17) MOVE-CORRESPONDING without additions` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc is converted to string. Note that the two
" components in component substruc-comp2 of gs_deep1 are drawn
" together when being converted to string.
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. Value assignment, for example,
" for col2 in gs_deep2-itab: Despite the fact that there is no
" identically named component col1 in the target structure,
" values are assigned starting with the first column of the source
" structure.
MOVE-CORRESPONDING gs_deep1 TO gs_deep2.
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `18) MOVE-CORRESPONDING with the ` &&
`EXPANDING NESTED TABLES addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. Due to the addition EXPANDING NESTED TABLES, the
" value assignment happens for identically named components. Hence,
" only col2 as the only shared and identically named component is
" filled.
MOVE-CORRESPONDING gs_deep1 TO gs_deep2 EXPANDING NESTED TABLES.
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `19) MOVE-CORRESPONDING with the` &&
` KEEPING TARGET LINES addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is kept due to the addition KEEPING
" TARGET LINES and content of gs_deep1-itab is added. The value
" assignment concerning the added lines happens like the
" MOVE-CORRESPONDING statement without addition. That is, despite
" the fact that there is no identically named component col1 in
" the target structure, values are assigned starting with the
" first column of the source structure.
MOVE-CORRESPONDING gs_deep1 TO gs_deep2 KEEPING TARGET LINES.
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `20) MOVE-CORRESPONDING with the ` &&
`EXPANDING NESTED TABLES KEEPING TARGET LINES addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is kept due to the addition KEEPING
" TARGET LINES. Content of gs_deep1-itab is added. The value
" assignment concerning the added lines happens like the
" MOVE-CORRESPONDING statement with the addition EXPANDING NESTED
" TABLES. That is, the value assignment happens for identically
" named components. Hence, only col2 as the only shared and
" identically named component is filled.
MOVE-CORRESPONDING gs_deep1 TO gs_deep2
EXPANDING NESTED TABLES KEEPING TARGET LINES.
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `21) CORRESPONDING operator without additions` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" initialized.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. Note the value assignment, for example, for col2
" in gs_deep2-itab. Despite the fact that there is no identically
" named component comp1 in the target structure, values are
" assigned starting with the first column of the source structure.
gs_deep2 = CORRESPONDING #( gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `22) CORRESPONDING operator with the` &&
` DEEP addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" initialized.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. Due to the addition DEEP, the value assignment
" happens for identically named components in the nested table.
" Hence, only col2 as the only shared and identically named
" component is filled.
gs_deep2 = CORRESPONDING #( DEEP gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `23) CORRESPONDING operator with the` &&
` BASE addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. The value assignment in the nested table happens
" like using the CORRESPONDING operator without addition. Note the
" value assignment, for example, for col2 in gs_deep2-itab.
" Despite the fact that there is no identically named component
" col1 in the target structure, values are assigned starting with
" the first column of the source structure.
gs_deep2 = CORRESPONDING #( BASE ( gs_deep2 ) gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `24) CORRESPONDING operator with the ` &&
`DEEP BASE addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is replaced by table content of
" gs_deep1-itab. The value assignment in the nested table happens
" like using the CORRESPONDING operator with the addition DEEP.
" That is, the value assignment happens for identically named
" components in the nested table. Hence, only col2 as the only
" shared and identically named component is filled.
gs_deep2 = CORRESPONDING #( DEEP BASE ( gs_deep2 ) gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `25) CORRESPONDING operator with the ` &&
`APPENDING addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is kept and content of gs_deep1-itab is
" added. The value assignment concerning the added lines happens
" like using the CORRESPONDING operator without addition. Note the
" value assignment, for example, for col2 in gs_deep2- itab.
" Despite the fact that there is no identically named component
" col1 in the target structure, values are assigned starting with
" the first column of the source structure.
gs_deep2 = CORRESPONDING #( APPENDING ( gs_deep2 ) gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
fill_deep_structures( ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `26) CORRESPONDING operator with the ` &&
`DEEP APPENDING addition` ) ).
"Notes on the result:
"- Existing content of identically named components is replaced.
"- Content in nonidentical components of the target structure is
" kept.
"- Substructure substruc: Same as above
"- Content of gs_deep2-itab is kept and content of gs_deep1-itab is
" added. The value assignment concerning the added lines happens
" like using the CORRESPONDING operator with the addition DEEP.
" That is, the value assignment happens for identically named
" components in the nested table. Hence, only col2 as the only
" shared and identically named component is filled.
"- It has the same effect as using DEEP APPENDING BASE.
gs_deep2 = CORRESPONDING #( DEEP APPENDING ( gs_deep2 )
gs_deep1 ).
out->write( data = gs_deep2 name = `gs_deep2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `27) Clearing individual components of a ` &&
`structure and the complete structure` ) ).
"Clearing individual component
CLEAR gs_struc-char1.
out->write( data = gs_struc name = `gs_struc` ).
out->write( |\n| ).
"Clearing the whole structure
CLEAR gs_struc.
out->write( data = gs_struc name = `gs_struc` ).
"Note: An assignment using the VALUE operator without entries in the parentheses clears the structure.
ls_flat_address = VALUE #( name = `Mr. Duncan Pea` ).
ls_flat_address = VALUE #( ).
ASSERT ls_flat_address IS INITIAL.
"The same applies to data reference variables pointing to structures.
addr_ref2 = NEW struc_type( name = `Mr. Duncan Pea` ).
addr_ref2 = NEW #( ).
ASSERT addr_ref2->* IS INITIAL.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Processing structures` ) ).
out->write( |Reading a row from a database table into a structure ...\n\n| ).
out->write( |28) ... that has a compatible type\n\n| ).
"The first entry that is found according to the WHERE condition is
"returned. Instead of creating a structure having a compatible type,
"the structure can be declared inline.
DATA ls_flsch1 TYPE zdemo_abap_flsch.
SELECT SINGLE FROM zdemo_abap_flsch
FIELDS *
WHERE carrid = 'LH' AND connid = '0400'
INTO @ls_flsch1.
SELECT SINGLE FROM zdemo_abap_flsch
FIELDS *
WHERE carrid = 'LH' AND connid = '0400'
INTO @DATA(ls_flsch2).
out->write( data = ls_flsch1 name = `ls_flsch1` ).
out->write( |\n| ).
out->write( data = ls_flsch2 name = `ls_flsch2` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `29) ... that has a different type` ) ).
"Creating structure having a different type.
DATA: BEGIN OF ls_fli_diff,
carrid TYPE zdemo_abap_flsch-carrid,
connid TYPE zdemo_abap_flsch-connid,
countryfr TYPE zdemo_abap_flsch-countryfr,
cityfrom TYPE zdemo_abap_flsch-cityfrom,
countryto TYPE zdemo_abap_flsch-countryto,
cityto TYPE zdemo_abap_flsch-cityto,
fldate TYPE zdemo_abap_fli-fldate,
END OF ls_fli_diff.
SELECT SINGLE FROM zdemo_abap_flsch
FIELDS *
WHERE carrid = 'JL' AND connid = '0408'
INTO CORRESPONDING FIELDS OF @ls_fli_diff.
out->write( data = ls_fli_diff name = `ls_fli_diff` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Reading a line from an internal table into a structure ...` ) ).
out->write( |30) ... using a SELECT statement\n\n| ).
"Creating and filling an internal table to be read from
DATA itab TYPE TABLE OF zdemo_abap_flsch WITH EMPTY KEY.
SELECT FROM zdemo_abap_flsch
FIELDS *
WHERE carrid = 'LH' ORDER BY PRIMARY KEY
INTO TABLE @itab
UP TO 4 ROWS.
"Reading from an internal table
SELECT SINGLE FROM @itab AS itab
FIELDS *
WHERE carrid = 'LH'
INTO @DATA(ls_select_itab).
out->write( data = ls_select_itab name = `ls_select_itab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `31) ... using a READ TABLE statement` ) ).
"The example shows the reading of one line into a work area, field
"symbol and a data reference variable, all representing structured
"data objects and declared inline below. Here, the reading of a
"line is based on the line number by specifying INDEX.
"Copying line into a work area
READ TABLE itab INTO DATA(ls_read_table) INDEX 1.
"Assignment to a field symbol
READ TABLE itab ASSIGNING FIELD-SYMBOL(<fs1>) INDEX 2.
"Reading into a data reference variable
READ TABLE itab REFERENCE INTO DATA(dref) INDEX 3.
out->write( data = ls_read_table name = `ls_read_table` ).
out->write( |\n| ).
out->write( data = <fs1> name = `<fs1>` ).
out->write( |\n| ).
out->write( data = dref->* name = `dref->*` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `32) ... using a table expression` ) ).
"The line number, that is, the index, is specified in square
"brackets.
DATA(ls_table_exp) = itab[ 3 ].
out->write( data = ls_table_exp name = `ls_table_exp` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Sequentially reading ...` ) ).
out->write( |33) ... a row from a database table into a structure\n\n| ).
"In the given simple example, the line that is found and returned
"in a structure, that is declared inline, is simply added to an
"internal table.
SELECT FROM zdemo_abap_flsch
FIELDS *
WHERE carrid = 'AZ'
INTO @DATA(ls_sel_loop).
IF sy-subrc = 0.
APPEND ls_sel_loop TO itab.
ENDIF.
ENDSELECT.
out->write( data = itab name = `itab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `34) ... a line from an internal table into a structure` ) ).
"The given example covers the reading of a line into a field symbol.
"Within the loop, a modification is carried out on a component
"of the structures.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs_loop>) WHERE carrid <> 'LH'.
<fs_loop>-carrid = 'XY'.
ENDLOOP.
out->write( data = itab name = `itab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `35) Inserting a single row ` &&
`into a database table from a structure` ) ).
"The statements in the given example can be considered as
"alternatives. The third statement demonstrates that the structure
"might also be created and filled in place instead of inserting a
"line from an existing structure.
DATA ls_struc_db TYPE zdemo_abap_tab1.
ls_struc_db = VALUE #( key_field = 1
char1 = 'aaa'
char2 = 'bbb'
num1 = 2
num2 = 3 ).
INSERT INTO zdemo_abap_tab1 VALUES @ls_struc_db.
"Structure filled anew with new primary key to
"avoid duplicate key error.
ls_struc_db = VALUE #( key_field = 2
char1 = 'ccc'
char2 = 'ddd'
num1 = 4
num2 = 5 ).
INSERT zdemo_abap_tab1 FROM @ls_struc_db.
INSERT zdemo_abap_tab1 FROM @( VALUE #( key_field = 3
char1 = 'eee'
char2 = 'fff'
num1 = 6
num2 = 7 ) ).