So my task has been to join several tables together so that the accountants can check account assignments. They wanted several fields from a total of 6 tables. It is difficult to do this because of needing the account assignment description and the valuation class so that nothing is left out when joining these tables. The code below works, but is getting a great deal of information because they want to be able to run it wide open and sort it after using the ALV Grid. The only filter on the join that is mandatory is the language, but obviously running this without anything else takes a long time. I have debugged this and I know that the LOOP AT statements are what is causing this to run so slow. I'm trying to figure out a way to optimize this, maybe with a SELECT...ENDSELECT loop and do it all at once, but I am pretty new to ABAP and am having trouble getting started. If anyone could give assistance that would be great! Thanks!
*Put data into internal table (Plant, Mat No, Mat Type, Mat Stat, Mat Desc, Valuation Class, Procurement Type, Sales Org, Dist Ch, Div ID, AAG, and AAG Desc
SELECT mvke~matnr mvke~vkorg mvke~vtweg mvke~ktgrm mara~mtart mara~spart makt~maktx marc~werks marc~mmsta marc~beskz marc~sobsl
FROM ( mvke
INNER JOIN mara
ON mara~matnr = mvke~matnr
INNER JOIN makt
ON makt~matnr = mvke~matnr
INNER JOIN marc
ON marc~matnr = mvke~matnr )
INTO CORRESPONDING FIELDS OF TABLE dt_materials
WHERE marc~werks IN werks
AND makt~spras = spras
AND mvke~ktgrm IN ktgrm
AND mvke~matnr IN matnr
AND mvke~vkorg IN vkorg
AND mvke~vtweg IN vtweg.
* Gets Account Assignment Group Description
LOOP AT dt_materials INTO ds_materials.
tab_index = sy-tabix.
SELECT SINGLE tvkmt~vtext
INTO ds_materials-vtext
FROM tvkmt
WHERE tvkmt~ktgrm = ds_materials-ktgrm
AND tvkmt~spras = spras.
SELECT SINGLE bklas
INTO ds_materials-bklas
FROM mbew
WHERE matnr = ds_materials-matnr
AND bwkey = ds_materials-werks.
IF sy-subrc EQ 0.
MODIFY dt_materials FROM ds_materials INDEX tab_index TRANSPORTING vtext bklas.
ENDIF.
ENDLOOP.