I need to pull data from 2 tables into 1 object. Both tables share equipment number, and I need to get the corresponding description from table EQKT and the owner ID from EQUZ.
DATA: BEGIN OF t_report OCCURS 3,
matnr LIKE eqkt-equnr,
mtart LIKE eqkt-eqktx,
maktx LIKE equz-J_3GEIGNER,
END OF t_report.*-
DATA: d_repid LIKE sy-repid.
*-----------------------------------------
*--Selection Screen
SELECT-OPTIONS: s_matnr FOR eqkt-equnr.
*-----------------------------------------
START-OF-SELECTION.
*-Read data
SELECT * FROM eqkt
WHERE equnr IN s_matnr.
CLEAR makt.
SELECT SINGLE *
FROM makt
WHERE matnr = eqkt-equnr AND
spras = sy-langu.
MOVE: eqkt-equnr TO t_report-matnr,
eqkt-eqktx TO t_report-mtart.
APPEND t_report.
ENDSELECT.
*This is where it stops working.
SELECT * FROM EQUZ
WHERE equnr IN s_matnr.
MOVE: EQUZ-J_3GEIGNER TO t_report-maktx.
APPEND t_report.
ENDSELECT.
The code compiles, and in the ALV in which I display it, it does properly display the "owner" at the top of the table, but no data is filled in for the owner (the equipment number and description are properly displayed)
The second loop should not add new entries into the table, but it should rather fill the third column in the table, which was basically populated in the first loop, correct?
If this is so, move the second select into the first select, and do it the same way as you do it with table MAKT
, namely
SELECT SINGLE J_3GEIGNER
FROM EQUZ
INTO T_REPORT-MAKTX
WHERE equnr = eqkt-equnr.
But if you want really to keep two loops, you need to use field-symbols in the second loop, like this:
FIELD-SYMBOLS: <T_REPORT> LIKE LINE OF T_REPORT.
SELECT * FROM EQUZ WHERE equnr IN s_matnr.
READ TABLE T_REPORT ASSIGNING <T_REPORT>
WITH KEY MATNR = EQUZ-EQUNR.
CHECK SY-SUBRC EQ 0. " skip unknown material numbers
<T_REPORT>-MAKTX = EQUZ-J_3GEIGNER.
ENDSELECT.
This way the values from table EQUZ
will be added to the proper record in the internal table T_RECORD
.
Btw, one additional hint: do not use OCCURS
, rather declare the tables explicitly. In your case it should be like this:
DATA: BEGIN OF S_report,
matnr LIKE eqkt-equnr,
mtart LIKE eqkt-eqktx,
maktx LIKE equz-J_3GEIGNER,
END OF S_report,
T_REPORT like standard table of S_REPORT.
Of course, then you can't use header line, but use S_REPORT
as work-area and for instance write APPEND S_RECORD TO T_RECORD
instead of APPEND T_RECORD
(but it will make your code only better and easier to read and maintain).