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

Merging Internal Tables

Former Member
0 Likes
772

Hi Guys,

Is it possible to merge internal tables having different fields?

If so, how?

Thanks a lot!

Hi Guys,

Is it possible to merge internal tables having different fields?

If so, how?

Thanks a lot!

6 REPLIES 6
Read only

Former Member
0 Likes
741

hi,

do this way ....

declare a final internal table having both the internal table fields ...and then do as below...



loop at itab1.
 move-corresponding fields of itab1 to it_final.
 append it_final.
 clear    it_final.
endloop.

loop at itab2.
 move-corresponding fields of itab2 to it_final.
 append it_final.
 clear    it_final.
endloop.

Read only

Former Member
0 Likes
741

Hi,

As far as I know.

If you want to merge two internal table there should be atleast one key field in common in both of those internal tables, otherwise you can't do it.

If you want to append all the fields into one final internal table then declare the internal table with all the fileds and use move corresponding to do it.

Read only

0 Likes
741

Hi,

Thanks a lot!

I have 3 internal tables:

i_crhd with fields:

kostl

objid

vgwts

arbpl

i_mseg with fields:

mblnr

bwart

matnr

werks

dmbtr

menge

meins

aufnr

kostl

budat

i_afko with fields:

aufnr

plnbez

aufpl

rsnum

I want to merge the 3 internal tables...

How am I going to do that? Thanks so much!

Read only

0 Likes
741

Hi,

What would hapen if my first internal table have 50 rows and the my cond internla table have 100 rows and my third have 50 rows...? How many rows will my final internal table have?

Thanks a lot guys...

Read only

Former Member
0 Likes
741

Hi Mark,

i think its possible to merge more than two internal tables. Pls find the below code depicting the same.

report zbnstest.

data syrepid like sy-repid.

data sydatum(10). " LIKE sy-datum.

data sypagno(3) type n.

type-pools : slis.

data: begin of t_mara occurs 0,

matnr like mara-matnr,

meins like mara-meins,

mtart like mara-mtart,

matkl like mara-matkl,

end of t_mara.

data : begin of t_marc occurs 0,

matnr like mara-matnr,

werks like marc-werks,

minbe like marc-minbe.

data: end of t_marc.

data : begin of t_makt occurs 0,

matnr like mara-matnr,

maktx like makt-maktx,

spras like makt-spras,

end of t_makt.

data: begin of itab1 occurs 0,

matnr like mara-matnr,

meins like mara-meins,

maktx like makt-maktx,

spras like makt-spras,

werks like marc-werks,

minbe like marc-minbe,

end of itab1.

data: fieldcatalog type slis_t_fieldcat_alv with header line,

fieldlayout type slis_layout_alv.

data : eventstab type slis_t_event with header line.

data : heading type slis_t_listheader with header line.

data : heading1 type slis_t_listheader with header line.

data : heading2 type slis_t_listheader with header line.

data : heading3 type slis_t_listheader with header line.

data : heading4 type slis_t_listheader with header line.

data : heading5 type slis_t_listheader with header line.

data : heading6 type slis_t_listheader with header line.

data : heading7 type slis_t_listheader with header line.

data : heading8 type slis_t_listheader with header line.

initialization.

syrepid = sy-repid.

sypagno = sy-pagno.

clear fieldcatalog.

start-of-selection.

perform fill_colorstruct using colorstruct.

perform populate_fieldcatalog.

perform selectdata_and_sort.

perform populate_layout using fieldlayout.

perform merge_fieldcatalog.

perform fill_eventstab tables eventstab.

perform fill_headingtable tables heading using 'HEADING'.

perform fill_headingtable tables heading1 using 'HEADING1'.

perform fill_headingtable tables heading2 using 'HEADING2'.

perform fill_headingtable tables heading3 using 'HEADING3'.

perform fill_headingtable tables heading4 using 'HEADING4'.

perform fill_headingtable tables heading5 using 'HEADING5'.

perform fill_headingtable tables heading6 using 'HEADING6'.

perform fill_headingtable tables heading7 using 'HEADING7'.

perform fill_headingtable tables heading8 using 'HEADING8'.

perform display_alv_list.

form populate_fieldcatalog.

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MATNR' 'X' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MEINS' ' '.

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MAKTX' ' ' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MTART' ' ' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MATKL' ' ' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'SPRAS' ' ' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'WERKS' ' ' .

perform fill_fields_of_fieldcatalog tables fieldcatalog

using 'ITAB1' 'MINBE' ' ' .

endform. " POPULATE_FIELDCATALOG

form fill_fields_of_fieldcatalog tables fieldcatalog

structure fieldcatalog

using p_tabname

p_fieldname

p_key.

  • p_no_out.

fieldcatalog-tabname = p_tabname.

fieldcatalog-fieldname = p_fieldname.

fieldcatalog-key = p_key.

fieldcatalog-emphasize = '1234'.

*fieldcatalog-no_out = p_no_out.

append fieldcatalog.

endform. " FILL_FIELDSOFFIELDCATALOG

form populate_layout using fieldlayout type slis_layout_alv.

fieldlayout-f2code = '&ETA' .

fieldlayout-zebra = 'X'.

  • FOR THE WINDOW TITLE.

fieldlayout-window_titlebar = 'ALV with Events'.

fieldlayout-colwidth_optimize = 'X'.

fieldlayout-no_vline = ' '.

*fieldlayout-no_input = 'X'.

fieldlayout-confirmation_prompt = ''.

fieldlayout-key_hotspot = 'X'.

  • This removes the column headings if the flag is set to 'X'

fieldlayout-no_colhead = ' '.

*fieldlayout-hotspot_fieldname = 'MAKTX'.

fieldlayout-detail_popup = 'X'.

  • fieldlayout-coltab_fieldname = 'X'.

endform. " POPULATE_LAYOUT

form selectdata_and_sort.

select matnr meins mtart matkl from mara

into corresponding fields of t_mara

up to 500 rows .

select matnr maktx spras from makt

into corresponding fields of t_makt

where matnr = t_mara-matnr and

spras = sy-langu.

select matnr werks minbe from marc

into corresponding fields of t_marc

where matnr = t_mara-matnr.

append t_marc.

endselect.

append t_makt.

endselect.

append t_mara.

endselect.

perform populate_itab1.

sort itab1 by matnr.

endform. " SELECTDATA_AND_SORT

form merge_fieldcatalog.

call function 'REUSE_ALV_FIELDCATALOG_MERGE'

exporting

i_program_name = syrepid

i_internal_tabname = 'ITAB1'

  • i_structure_name = 'COLORSTRUCT'

  • I_CLIENT_NEVER_DISPLAY = 'X'

i_inclname = syrepid

changing

ct_fieldcat = fieldcatalog[]

exceptions

inconsistent_interface = 1

program_error = 2

others = 3.

endform. " MERGE_FIELDCATALOG

form display_alv_list.

call function 'REUSE_ALV_LIST_DISPLAY'

exporting

  • I_INTERFACE_CHECK = ' '

i_callback_program = syrepid

  • I_CALLBACK_PF_STATUS_SET = ' '

  • I_CALLBACK_USER_COMMAND = ' '

i_structure_name = 'ITAB1'

is_layout = fieldlayout

it_fieldcat = fieldcatalog[]

  • IT_EXCLUDING =

  • IT_SPECIAL_GROUPS =

  • IT_SORT =

  • IT_FILTER =

  • IS_SEL_HIDE =

  • I_DEFAULT = 'X'

  • THE FOLLOWING PARAMETER IS SET AS 'A' INORDER TO DISPLAY THE STANDARD

  • TOOL BAR

i_save = 'A'

  • IS_VARIANT = ' '

it_events = eventstab[]

  • IT_EVENT_EXIT =

  • IS_PRINT =

  • I_SCREEN_START_COLUMN = 0

  • I_SCREEN_START_LINE = 0

  • I_SCREEN_END_COLUMN = 0

  • I_SCREEN_END_LINE = 0

  • IMPORTING

  • E_EXIT_CAUSED_BY_CALLER =

  • ES_EXIT_CAUSED_BY_USER =

tables

t_outtab = itab1

exceptions

program_error = 1

others = 2.

endform. " DISPLAY_ALV_LIST

form populate_itab1.

loop at t_mara.

loop at t_makt where matnr = t_mara-matnr.

loop at t_marc where matnr = t_mara-matnr.

move-corresponding t_mara to itab1.

move-corresponding t_makt to itab1.

move-corresponding t_marc to itab1.

append itab1.

endloop.

endloop.

endloop.

endform. " POPULATE_ITAB1

form fill_eventstab tables p_eventstab structure eventstab.

  • WHEN THE FOLLOWING FUNCTION IS CALLED THE SYSTEM POPULATES THE

  • INTERNAL TABLE EVENTSTAB WITH A LIST OF EVENTS NAME.

  • AS SHOWN BELOW WHEN USING I_LIST_TYPE = 0 THE FUNCTION RETURNS 14

  • EVENTS NAME.

call function 'REUSE_ALV_EVENTS_GET'

exporting

i_list_type = 0

importing

et_events = p_eventstab[]

exceptions

list_type_wrong = 1

others = 2.

read table p_eventstab with key name = slis_ev_top_of_page.

if sy-subrc = 0 .

move 'TOP_OF_PAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_top_of_coverpage.

if sy-subrc = 0 .

move 'TOP_OF_COVERPAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_end_of_coverpage .

if sy-subrc = 0 .

move 'END_OF_COVERPAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_foreign_top_of_page.

if sy-subrc = 0 .

move 'FOREIGN_TOP_OF_PAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_foreign_end_of_page.

if sy-subrc = 0 .

move 'FOREIGN_END_OF_PAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_list_modify.

if sy-subrc = 0 .

move 'LIST_MODIFY' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_top_of_list.

if sy-subrc = 0 .

move 'TOP_OF_LIST' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_end_of_page.

if sy-subrc = 0 .

move 'END_OF_PAGE' to p_eventstab-form.

append p_eventstab.

endif.

read table p_eventstab with key name = slis_ev_end_of_list .

if sy-subrc = 0 .

move 'END_OF_LIST' to p_eventstab-form.

append p_eventstab.

endif.

endform. " FILL_EVENTSTAB

form fill_headingtable tables p_heading structure heading

using tablename.

case tablename.

when 'HEADING'.

p_heading-typ = 'H'.

concatenate

' REPORT NAME:-' syrepid

' ABB Industry Pte Ltd' into p_heading-info.

append p_heading.

write sy-datum using edit mask '__/__/____' to sydatum.

concatenate

' DATE:-' sydatum ' USER: ' sy-uname 'PAGE NO:' sypagno

into p_heading-info.

append p_heading.

when 'HEADING1'.

p_heading-typ = 'H'.

p_heading-info = 'TOP-OF-COVER-PAGE'.

append p_heading.

when 'HEADING2'.

p_heading-typ = 'H'.

p_heading-info = 'END-OF-COVER-PAGE'.

append p_heading.

when 'HEADING3'.

p_heading-typ = 'H'.

p_heading-info = 'FOREIGN-TOP-OF-PAGE'.

append p_heading.

when 'HEADING4'.

p_heading-typ = 'H'.

p_heading-info = 'FOREIGN-END-OF-PAGE'.

append p_heading.

  • WHEN 'HEADING5'.

  • P_HEADING-TYP = 'H'.

  • P_HEADING-INFO = 'LIST-MODIFY'.

  • APPEND P_HEADING.

when 'HEADING6'.

p_heading-typ = 'H'.

p_heading-info = 'END-OF-PAGE'.

append p_heading.

when 'HEADING7'.

p_heading-typ = 'H'.

p_heading-info = 'END-OF-LIST'.

append p_heading.

when 'HEADING8'.

p_heading-typ = 'H'.

p_heading-info = 'TOP-OF-LIST'.

append p_heading.

endcase.

endform. " FILL_HEADINGTABLE

form top_of_page.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading[]

exceptions

others = 1.

endform.

form fill_colorstruct using p_colorstruct type slis_coltypes .

p_colorstruct-heacolfir-col = 6.

p_colorstruct-heacolfir-int = 1.

p_colorstruct-heacolfir-inv = 1.

endform. " FILL_COLORSTRUCT

form top_of_coverpage.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading1[]

exceptions

others = 1.

endform.

form end_of_coverpage.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading2[]

exceptions

others = 1.

endform.

form foreign_top_of_page.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading3[]

exceptions

others = 1.

endform.

form foreign_end_of_page.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading4[]

exceptions

others = 1.

endform.

form end_of_page.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading6[]

exceptions

others = 1.

endform.

form end_of_list.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading7[]

exceptions

others = 1.

endform.

form top_of_list.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = heading8[]

exceptions

others = 1.

endform.

*--- End of Program

Pls reward if useful,

THanks,

Sirisha.

Edited by: Sirisha Vadlamani on Apr 15, 2008 5:57 AM

Read only

Former Member
0 Likes
741

Hi,

u can merge internal tables . take internal tables ITAB1, ITAB2 and final internal table ITAB3 with all the fields in ITAB1 and ITAB2.

it is well and good if u have alteast field common in both the internal tables ITAB1 and ITAB2.

generally we merge the internal tables with atleast one field in common.

we can also merge internal tables with fields not in common.

take internal tables ITAB1 and ITAB2.

ITAB3 is final internal table with all the fields of ITAB1 and ITAB2.

loop the final table to write move corresponding ITAB1 to ITAB3. and ITAB2 to ITAB3.

REWARD IF HELPFUL

RAAM.