‎2006 Dec 04 5:11 PM
Hello all,
I am asked to create a program to extract some material information from R/3 system. The information are from several fields of tables MARM, MARA, MAKT..
Anyone can give me a sample code? The fields include Unit of Measure, material names, etc.
What's the logic?
Do I need to create an internal table? Select all the fields from tables and put them into the internal table, loop the internal table, output it to external table? where to output?
Thanks for help,
Linda
Message was edited by:
Linda Tseng
‎2006 Dec 04 5:21 PM
Hi Linda,
You could create an internal table that is comprised of all the fields you require. You need to determine from what tables the information is comming from and how to drill down to obtain the correct infomation.
logic:
select * (or single *) from MARM into table I_MARM where .....
select * (or single *) from MARA into table I_MARA where .....
select * (or single *) from MAKT into table I_MAKT where .....
once you have your internal tables filled....
LOOP AT I_MARM WHERE ....
READ TABLE I_MARA WITH KEY
VARIABLE = I_MARM-VARIABLE.
IF SY-SUBRC = 0. <--IF A RECORD IS FOUND
READ TABLE I_MAKT WITH KEY
VARIABLE = I_MARA-VARIABLE.
IF SY-SUBRC = 0.
MY_RESULTS-VAR1 = I_MARM-VARIABLE
MY_RESULTS-VAR2 = I_MARA-VARIABLE
MY_RESULTS-VAR3 = I_MAKT-VARIABLE
ENDIF.
ENDIF.
ENDLOOP.
hope this helps.
Warren
‎2006 Dec 04 5:16 PM
create internal tables...get data from tbales...loop one table & read other tables with keys and get all required data into final internal table...Use this for eport output.
‎2006 Dec 04 5:17 PM
Here is a sample program.
REPORT ZRICH_0003 .
* Tables.
TABLES: MARA.
* Global ALV Data Declarations
TYPE-POOLS: SLIS.
* Internal Tables
DATA: BEGIN OF ITAB OCCURS 0,
MATNR TYPE MARA-MATNR,
MAKTX TYPE MAKT-MAKTX,
END OF ITAB.
DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
* Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002 .
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
SELECTION-SCREEN END OF BLOCK B1.
START-OF-SELECTION.
PERFORM GET_DATA.
PERFORM CALL_ALV.
*********************************************************************
* Form GET_DATA
*********************************************************************
FORM GET_DATA.
SELECT MARA~MATNR MAKT~MAKTX
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM MARA
INNER JOIN MAKT
ON MARA~MATNR = MAKT~MATNR
WHERE MARA~MATNR IN S_MATNR
AND MAKT~SPRAS = SY-LANGU.
ENDFORM.
************************************************************************
* CALL_ALV
************************************************************************
FORM CALL_ALV.
DATA: VARIANT TYPE DISVARIANT.
VARIANT-REPORT = SY-REPID.
VARIANT-USERNAME = SY-UNAME.
PERFORM BUILD_FIELD_CATALOG.
* Call ABAP List Viewer (ALV)
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = FIELDCAT
IS_VARIANT = VARIANT
I_SAVE = 'U'
TABLES
T_OUTTAB = ITAB.
ENDFORM.
************************************************************************
* BUILD_FIELD_CATALOG
************************************************************************
FORM BUILD_FIELD_CATALOG.
DATA: FC_TMP TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
DATA: LABEL(30) TYPE C.
CLEAR: FIELDCAT. REFRESH: FIELDCAT.
PERFORM UPDATE_CATALOG USING 'Material Number'
'ITAB'
'MATNR'
'18'
SPACE
SPACE
'L'.
PERFORM UPDATE_CATALOG USING 'Material Description'
'ITAB'
'MAKTX'
'40'
SPACE
SPACE
'L'.
ENDFORM.
************************************************************************
* UPDATE_CATALOG
************************************************************************
FORM UPDATE_CATALOG USING COL_HEAD
TABLE
FIELD
OUTPUTLEN
DO_SUM
NO_OUT
JUST.
DATA: TMP_FC TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
TMP_FC-REPTEXT_DDIC = COL_HEAD.
TMP_FC-FIELDNAME = FIELD.
TMP_FC-TABNAME = TABLE.
TMP_FC-OUTPUTLEN = OUTPUTLEN.
TMP_FC-JUST = JUST.
TMP_FC-DECIMALS_OUT = 0.
TMP_FC-DO_SUM = DO_SUM.
TMP_FC-NO_OUT = NO_OUT.
APPEND TMP_FC TO FIELDCAT.
ENDFORM.
Regards,
Rich Heilman
‎2006 Dec 04 6:17 PM
Hi Rich,
Could you please explain what function module 'REUSE_ALV_GRID_DISPLAY' does? Also what do BUILD_FIELD_CATALOG, UPDATE_CATALOG do in your sample code? Appreciate!
Linda
‎2006 Dec 04 6:56 PM
The function module is the ALV grid function module which gives you the nice output screen, which allows for sorting, filtering, and other things., BUILD_FIELDCATALOG is a form routine that builds the FC, which tells the ALV function module what columns it should display, usually it is the same columns as in the internal table with the data. UPDATE_CATALOG is actually just another form routine which abstracts the actually updating of the FC, you don't have to use it this way, you can simply update directly.
Regards,
Rich Heilman
‎2006 Dec 04 7:46 PM
‎2006 Dec 04 5:17 PM
Hi,
Select all necessary info from MARA & MARM in one internal table joining matnr.
Then update material description from makt for the same matnr in the same internal table .
M in hurry so unable to give u details code.
Cheers...Bye
‎2006 Dec 04 5:21 PM
Hi Linda,
You could create an internal table that is comprised of all the fields you require. You need to determine from what tables the information is comming from and how to drill down to obtain the correct infomation.
logic:
select * (or single *) from MARM into table I_MARM where .....
select * (or single *) from MARA into table I_MARA where .....
select * (or single *) from MAKT into table I_MAKT where .....
once you have your internal tables filled....
LOOP AT I_MARM WHERE ....
READ TABLE I_MARA WITH KEY
VARIABLE = I_MARM-VARIABLE.
IF SY-SUBRC = 0. <--IF A RECORD IS FOUND
READ TABLE I_MAKT WITH KEY
VARIABLE = I_MARA-VARIABLE.
IF SY-SUBRC = 0.
MY_RESULTS-VAR1 = I_MARM-VARIABLE
MY_RESULTS-VAR2 = I_MARA-VARIABLE
MY_RESULTS-VAR3 = I_MAKT-VARIABLE
ENDIF.
ENDIF.
ENDLOOP.
hope this helps.
Warren
‎2006 Dec 04 5:26 PM
‎2006 Dec 04 5:50 PM
One question.
Based on the code, I extract the data out from those tables into the internal table. How to export the data from the internal table to a file? Is there a function module I can use? Thanks!
Linda
‎2006 Dec 04 6:03 PM
‎2006 Dec 04 6:24 PM
You could also use:
OPEN DATASET P_OUTF FOR OUTPUT IN TEXT MODE.
LOOP AT I_RESULTS.
TRANSFER I_RESULTS TO P_OUTF.
ENDLOOP.
CLOSE DATASET P_OUTF.
**p_outf being the parameter for the output on your selection screen
**I_results being your internal table.
‎2006 Dec 04 7:57 PM
Linda,
I should have mentioned earlier that you can use this to choose your file.
Place it between your initialization and start-of-selection.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_OUTF.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_OUTF.
‎2006 Dec 18 6:43 PM
Hi.
my question is kind if late.
Is there a specific format of the file name I want to save data to? I tried this code, but I always got error of no authorization? Thanks!
Linda