Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to Join Table Types?

Former Member
0 Likes
1,963

Hello,

How does one join multiple table types? Is this possible?  What is the best solution.

Things to know:  My level of ABAP experience is beginner.

One data source is returning data from a Function Group.  The other data source is select from tables.

These two sources must be joined.

Example Code:

...

DATA:  itab1 type table of structure_table,

             lty_itab1 type structure_table.

CALL FUNTION 'SD_FUNCTION_GROUP'

     EXPORTING

          PARAM1 = 'X'

     TABLES

          itab1 = itab1.

TYPES:  BEGIN OF GTY_OBJ,

                V_COL1 TYPE MATNR,

                 ...

               END OF GTY_OBJ.

DATA:  LTY_OBJ TYPE GTY_OBJ,

             LT_TAB TYPE STARDARD TABLE OF GTY_OBJ.

SELECT
LIPS~MATNR AS V_COL1

...

FROM LIPS

INTO CORRESPONDING FIELDS OF TABLE LT_TAB

WHERE ...

...

How does one now join the type table ITAB1 and LT_TAB?

Is this possible?  What method should be used?

I was looking for a general solution, rather than a solution to my problem.  If requested I can post my code.

Thanks for your kind reply.

Hello,

How does one join multiple table types? Is this possible?  What is the best solution.

Things to know:  My level of ABAP experience is beginner.

One data source is returning data from a Function Group.  The other data source is select from tables.

These two sources must be joined.

Example Code:

...

DATA:  itab1 type table of structure_table,

             lty_itab1 type structure_table.

CALL FUNTION 'SD_FUNCTION_GROUP'

     EXPORTING

          PARAM1 = 'X'

     TABLES

          itab1 = itab1.

TYPES:  BEGIN OF GTY_OBJ,

                V_COL1 TYPE MATNR,

                 ...

               END OF GTY_OBJ.

DATA:  LTY_OBJ TYPE GTY_OBJ,

             LT_TAB TYPE STARDARD TABLE OF GTY_OBJ.

SELECT
LIPS~MATNR AS V_COL1

...

FROM LIPS

INTO CORRESPONDING FIELDS OF TABLE LT_TAB

WHERE ...

...

How does one now join the type table ITAB1 and LT_TAB?

Is this possible?  What method should be used?

I was looking for a general solution, rather than a solution to my problem.  If requested I can post my code.

Thanks for your kind reply.

6 REPLIES 6
Read only

Former Member
0 Likes
1,353

Hi Bertram,

You have to use nested loop or combination of LOOP and READ to join internal tables.

Read only

Former Member
0 Likes
1,353

Create a new structure by including both types.

Types: begin of tab,

Include type1,Include type 2,

Endof tab.

Now createcreate an internal table of type above.

Loop at itab1.

Readtable lt_tab. "Use appropriate join condition here and then populate the newly created internal table

Read only

Former Member
0 Likes
1,353

Hi Betram,

What you are asking for is to combine the results in ITAB1 and LT_TAB. This does not mean joining two table types.

To achieve what you are looking for, you will need to declare one more Table Type and a corresponding internal table, say LT_JOIN, which contains all the UNIQUE columns from ITAB1 and LT_TAB.

Also, to be noted is that you would need a common key connecting both ITAB1 and LT_TAB, as in a common column, based on which you can join them.

Once the pre-requisites are set, you need to loop at the parent table and read the second table with the common key, then populate the new table.

e.g.: Assuming ITAB1 is the parent.

LOOP AT itab1 INTO wa_itab1.

     READ TABLE lt_tab INTO ls_tab WITH KEY column = wa_itab1-column.

     IF sy-subrc EQ 0.

          ls_join-column1 = wa_itab1-column1.

          ls_join-column2 = wa_itab1-column2.

          ls_join-column3 = wa_itab1-column2.

          ls_join-column4 = wa_itab1-column3.

          ls_join-column5 = ls_tab-column1.

          ls_join-column6 = ls_tab-column2.

          ls_join-column7 = ls_tab-column3.

          ls_join-column8 = ls_tab-column4.

          APPEND ls_join INTO lt_join.

     ENDIF.

     CLEAR:   ls_join,

                    wa_itab1,

                    ls_tab.

ENDLOOP.

Hope this helps.

Read only

0 Likes
1,353

For clarification,  I need to take elements from ITAB1 and LT_TAB and join based on a common key.

The process would not be combining the results of each.  The process needs to join the results of

each data set into a new data set.

George Abraham your solution is most helpful.  Thank you very much.

Read only

0 Likes
1,353

:Thank you


You are correct. You would be joining the results from ITAB1 with LT_TAB based on the common key.


For more explanation:


E.G.:


If ITAB1 contains Material No., Weight and Unit of Measurement and LT_TAB contains the Material Description and you want a final table which contains Material No., Material Description, Weight and Unit of Measurement,


Then while populating LT_TAB, you should also ensure that it contains Material No. with its corresponding Description.


Then LT_FINAL can be populated as:


LOOP AT itab1 INTO wa_itab1.

     READ lt_tab INTO ls_tab WITH KEY material_no. = wa_itab1-material_no.

     IF sy-subrc EQ 0.

          ls_final-material_no = wa_itab1-material_no.

          ls_final-description = ls_tab-material_description.

          ls_final-weight = wa_itab1-weight.

          ls_final-uom = wa_itab1-uom.

          APPEND ls_final TO lt_final.

     ENDIF.

     CLEAR:   ls_final,

                    wa_itab1,

                    ls_tab.

ENDLOOP.

Read only

Former Member
0 Likes
1,353

try this ...

pass both the internal table entries into one final internal table viz., say GT_final by declaring the data declaration as below.

DATA : gt_final type table of ty_final,

            gs_final type ty_final.

if not lt_tab is initial.

select * from <database table > into itab1 for all entries in lt_tab where < keyfield> = lt_tab-<fieldname>

loop at lt_tab into gty_obj.

gs_final -<fieldname> = gty_obj-<fieldname>

read table itab1 into lty_itab1 with key < fieldname > = lt_tab<fieldname>.

gs_final-<filedname> = lt_tab<fieldname>

endloop.

declare ALV for display...

Reward if useful.

Best rgds/thnks,

Srikanth.