需求描述:
开发一个报表程序,从MARA,MARC,MARD,MAKT4个表选取数据到内表,然后用ALV输出内表数据。
要求:
SELECTION-SCREEN:Material Number,Plant,Storage Location
Output ALV:Material Number,Material Type,Material Description,Plant,Storage Location,
Valuated Unrestricted-Use Stock,Stock in transfer ,Stock in Quality Inspection,
Total Stock of All Restricted Batches,Blocked Stock,Blocked Stock Returns
下面进入实战。
Tcode:SE38进入ABAP Editor:Initial Screen:
输入报表程序名,点击“Create”,首先根据Output ALV定义如下结构体类型:
TYPES:BEGIN OF GTY_TOTAL,
MATNR TYPE MARD-MATNR,"Material Number
MTART TYPE MARA-MTART,"Material Type
MAKTX TYPE MAKT-MAKTX,"Material Description
WERKS TYPE MARD-WERKS,"Plant
EKGRP TYPE MARC-EKGRP,"Purchaing group
LGORT TYPE MARD-LGORT,"Storage Location
LABST TYPE MARD-LABST,"Valuated Unrestricted-Use Stock
UMLME TYPE MARD-UMLME,"Stock in transfer
INSME TYPE MARD-INSME,"Stock in Quality Inspection
EINME TYPE MARD-EINME,"Total stock of All Restricted Batches
SPEME TYPE MARD-SPEME,"Blocked stock
RETME TYPE MARD-RETME,"Blocked stock Returns
END OF GTY_TOTAL.
然后定义程序后面要用到的工作区和内表:
DATA: GS_TOTAL TYPE GTY_TOTAL,
GT_TOTAL TYPE TABLE OF GTY_TOTAL,
GS_MARA TYPE MARA,
GT_MARA TYPE TABLE OF MARA,
GS_MARC TYPE MARC,
GT_MARC TYPE TABLE OF MARC,
GS_MARD TYPE MARD,
GT_MARD TYPE TABLE OF MARD,
GS_MAKT TYPE MAKT,
GT_MAKT TYPE TABLE OF MAKT.
再定义跟ALV相关的变量:
DATA:it_field TYPE slis_t_fieldcat_alv,
wa_field TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
下面定义选择界面:
SELECTION-SCREEN BEGIN OF BLOCK BK1 WITH FRAME TITLE TEXT-T01.
SELECT-OPTIONS: S_MATNR FOR MARD-MATNR,
S_WERKS FOR MARD-WERKS,S_LGORT FOR MARD-LGORT.
SELECTION-SCREEN END OF BLOCK BK1.
下面开始从数据库表取数到内表,这里我把取数写成了一个Function Module,Tcode:SE37进入Function Builder:Initial Screen:
输入Function Module,点击“Create”,
其中Associated Type是表类型,先在数据字典里定义一个Structure,然后在参考这个Structure定义的Table type。
Export参数如下:
其中Associated Type也是一个表类型。
下面是取数逻辑:
FUNCTION ZTRAINING_GET_DATA.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_MATNR) TYPE ZTYRANGEMATNR
*" REFERENCE(I_WERKS) TYPE ZTYRANGEWERKS
*" REFERENCE(I_LGORT) TYPE ZTYRANGELGORT
*" EXPORTING
*" REFERENCE(E_OUTTAB) TYPE ZTYOUTTABLE
*" EXCEPTIONS
*" ITAB_NULL
*" CALL_ERROR
*"----------------------------------------------------------------------
SELECT MATNR WERKS LGORT LABST UMLME INSME EINME SPEME RETME
FROM MARD
INTO CORRESPONDING FIELDS OF TABLE GT_MARD
WHERE MATNR IN I_MATNR
AND WERKS IN I_WERKS
AND LGORT IN I_LGORT.
IF GT_MARD IS NOT INITIAL.
SELECT MATNR MTART
FROM MARA
INTO CORRESPONDING FIELDS OF TABLE GT_MARA
FOR ALL ENTRIES IN GT_MARD
WHERE MATNR = GT_MARD-MATNR.
SORT GT_MARA BY MATNR.
SELECT MATNR WERKS
FROM MARC
INTO CORRESPONDING FIELDS OF TABLE GT_MARC
FOR ALL ENTRIES IN GT_MARD
WHERE MATNR = GT_MARD-MATNR
AND WERKS = GT_MARD-WERKS.
SORT GT_MARC BY MATNR WERKS.
SELECT MATNR MAKTX
FROM MAKT
INTO CORRESPONDING FIELDS OF TABLE GT_MAKT
FOR ALL ENTRIES IN GT_MARD
WHERE MATNR = GT_MARD-MATNR.
SORT GT_MAKT BY MATNR.
ELSE.
RAISE ITAB_NULL.
ENDIF.
LOOP AT GT_MARD INTO GS_MARD.
CLEAR GS_TOTAL.
MOVE-CORRESPONDING GS_MARD TO GS_TOTAL.
READ TABLE GT_MARA INTO GS_MARA
WITH KEY MATNR = GS_TOTAL-MATNR
BINARY SEARCH.
READ TABLE GT_MARC INTO GS_MARC
WITH KEY MATNR = GS_TOTAL-MATNR
WERKS = GS_TOTAL-WERKS
BINARY SEARCH.
READ TABLE GT_MAKT INTO GS_MAKT
WITH KEY MATNR = GS_TOTAL-MATNR
BINARY SEARCH.
IF SY-SUBRC = 0.
GS_TOTAL-MTART = GS_MARA-MTART.
GS_TOTAL-EKGRP = GS_MARC-EKGRP.
GS_TOTAL-MAKTX = GS_MAKT-MAKTX.
ENDIF.
APPEND GS_TOTAL TO E_OUTTAB.
ENDLOOP.
ENDFUNCTION.
然后在程序中调用Function Module:
START-OF-SELECTION.
CALL FUNCTION 'ZTRAINING_GET_DATA'
EXPORTING
I_MATNR = S_MATNR[]
I_WERKS = S_WERKS[]
I_LGORT = S_LGORT[]
IMPORTING
E_OUTTAB = GT_TOTAL
EXCEPTIONS
ITAB_NULL = 1
CALL_ERROR = 2
OTHERS = 3.
CASE SY-SUBRC.
* Implement suitable error handling here
WHEN 1.
WRITE: / 'GT_MARD is NULL!'.
WHEN 2.
WRITE: / '函数调用失败!'.
ENDCASE.
下面编辑ALV输出列:
PERFORM edit_fieldcat USING: 'WERKS' 'Plant' '1' ,
'MATNR' 'Material Number' '2' ,
'LGORT' 'Storage Location' '3' ,
'MTART' 'Material Type' '4' ,
'MAKTX' 'Material Description' '5' ,
'LABST' 'Valuated Unrestricted-Use Stock' '6' ,
'UMLME' 'Stock in transfer' '7' '',
'INSME' 'Stock in Quality Inspection' '8' ,
'EINME' 'Total stock of All Restricted Batches' '9' ,
'SPEME' 'Blocked stock' '10' ,
'RETME' 'Blocked stock returns' '11' .
FORM EDIT_FIELDCAT USING $i_fieldname
$i_seltext
$i_col.
CLEAR wa_field.
wa_field-fieldname = $i_fieldname.
wa_field-seltext_l = $i_seltext. "长文本
wa_field-col_pos = $i_col. "列的位置
APPEND wa_field TO it_field.
ENDFORM.
最后用ALV输出内表数据:
END-OF-SELECTION.
PERFORM OUTPUT_ALV.
FORM OUTPUT_ALV .
*DATA: w_repid TYPE sy-repid.
wa_layout-zebra = 'X'. "使ALV界面呈现颜色交替
wa_layout-colwidth_optimize = 'X'. "自动优化列宽
*w_repid = sy-repid. "当前程序的名称
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_grid_title = '物料详细信息表'
is_layout = wa_layout
it_fieldcat = it_field
TABLES
t_outtab = gt_total
EXCEPTIONS
ITAB_NULL = 1
CALL_ERROR = 2.
ENDFORM.
选择界面如下:
运行结果如下: