Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
Showing results for 
Search instead for 
Did you mean: 
Active Contributor

Here I am trying to explain how to do an Outer Join of 2 internal tables using the new internal table functions available in ABAP Release 7.40. The same method can be extended to add more tables to the join.

Sample program is below. I hope the inline comments are clear. If you need any explanation, please add a comment below and I will try to answer.

report outer_joins.

class lcl_outer_join definition.

   public section.


*Main method


   private section.

*Sample tables to hold initial values

     types:begin   of   struc1,

             f1 type numc1,

             f2 type numc1,

           end     of   struc1,

           begin   of   struc2,

             f1 type numc1,

             f3 type numc1,

           end     of   struc2,

*Table structure to hold output. Common key between the tables is field F1

           begin   of   struc3,

             f1 type numc1,

             f2 type numc1,

             f3 type numc1,

           end     of   struc3,

           tab1    type standard table of struc1 with non-unique key f1,

           tab2    type standard table of struc2 with non-unique key f1,

           tab3    type standard table of struc3 with non-unique key f1,

           ty_numc type numc1.

     data: table1 type tab1,

           strc2  type struc2,

           table2 type tab2.




       line_value importing value(key) type numc1 returning value(result) type numc1.


class lcl_outer_join implementation.

   method perform.

*Build input tables

     build_tables( ).

*Perform outer join

     outer_join( ).


   method build_tables.

*Populate initial values

*Reference: ABAP News for 7.40, SP08 - Start Value for Constructor Expressions

     table1 = value tab1( ( f1 = '1' f2 = '2' )

                          ( f1 = '2' f2 = '8' )

                          ( f1 = '1' f2 = '9' )

                          ( f1 = '3' f2 = '4' ) ).

     table2 = value tab2( ( f1 = '1' f3 = '5' )

                          ( f1 = '3' f3 = '6' ) ).


   method line_value.

*Store the last accessed structure, to reduce table access

     if strc2-f1 ne key.


*Read the line from 2nd table, with respect to the key

*Reference: ABAP News for Release 7.40 - Table Expressions

           strc2 = table2[ f1 = key ].

         catch cx_sy_itab_line_not_found.

*If corresponding line was not found, then avoid dump, populate blank

           clear strc2.

           strc2-f1 = key.



*Pass the required field from 2nd table as result

     result = strc2-f3.


   method outer_join.

*Perform join and display output

*Reference: ABAP News for 7.40, SP08 - FOR Expressions

     cl_demo_output=>display_data( value tab3( for data1 in table1

                                             ( f1 = data1-f1

                                               f2 = data1-f2

*Field F3, is populated by calling the method, passing the common key

                                               f3 = line_value( data1-f1 ) ) ) ).

*If you are sure that table2 will always have an entry corresponding to F1 field,

*then there is no need to create a method to read the value.

*Meaning: Since my table1 has a row with F1 = 2, but table2 doesn't,

*the statement below, will result in a dump with exception

*cx_sy_itab_line_not_found. But, if my table2 also

*had an entry corresponding to F1 = 2, then, this single statement

*is enough to perform the join.

    cl_demo_output=>display_data( value tab3( for data1 in table1

                                             ( f1 = data1-f1

                                               f2 = data1-f2

                                               f3 = table2[ f1 = data1-f1 ]-f3 ) ) ).




   data:   joins   type ref to lcl_outer_join.

*Initialize and perform join

   create object joins.

   joins->perform( ).

1 Comment