这是个完整的例子,适合初学者直接拷贝到abap编辑器直接运行,调试。
1、能学到abap代码编写的规范;
2、能学到内表命名规则
3、能学到程序运行的层次规律
4、能学到写简单的MESSAGE
5、能学到联合查询和单字段查询
6、能学到LOOP AT的使用
7、能学到函数是怎么调用
8、能学到简单的输出格式
*&--------------------------------------------------------------------------------*
*& PROGRAM NAME : ZUCP_MM_0001 *
*$ Version : 1.0 *
*& MODULE NAME : MM *
*& AUTHOR : XXX,XXXXX *
*& CREATE DATE : 2012-12-29 *
*& MODIFY REASON : XXXXXXXXXXXXXXXXXXXX *
*& MODIFY DATE : XXXXXXXXXX *
*& PROGRAM TYPE : REPORTS *
*& SAP RELEASE : 6.0 *
*& DESCRIPTION : 仓库库存报表 *
*&--------------------------------------------------------------------------------*
REPORT ZUCP_MM_0001
NO STANDARD PAGE HEADING
LINE-COUNT 66
LINE-SIZE 120.
*单元命名规则
*统一以ZUCP或ZWT开头后面跟模块名和程序流水号,测试程序加TEST(ZUCP_TEST_0001),
*修改程序时不可以在原程序上修改,要COPY原程序建一个新的程序,修改后的程序名
*从V2(ZUCP_MM_0001_V2)开始,依此类推.
*&--------------------------------------------------------------------------------*
*& TABLES:
*&--------------------------------------------------------------------------------*
TABLES: MARA,MAKT,MSEG.
*&--------------------------------------------------------------------------------*
*& PROGRAM VARIABLES OR INCLUDE
*&--------------------------------------------------------------------------------*
*&--------------------------------------------------------------------------------*
*& INTERNAL TABLES AND WORK AREAS *
*&--------------------------------------------------------------------------------*
*内表定义规则
*Internal tables (global):ITAB 需要输出的主内表统一命名为ITAB
*Internal Tables (Local) :ITAB_XXX 子内表
*工作区 WA_XXXX
DATA: BEGIN OF ITAB OCCURS 0, "主内表
MATNR LIKE MARA-MATNR,
MATKL LIKE MARA-MATKL,
MEINS LIKE MARA-MEINS,
MAKTX LIKE MAKT-MAKTX,
LABST LIKE MARD-LABST,
END OF ITAB.
*&--------------------------------------------------------------------------------*
*& SELECT SCREEN
*&--------------------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK A1 WITH FRAME TITLE T1.
PARAMETERS : P_MATNR LIKE MARA-MATNR OBLIGATORY DEFAULT '王石田朴珺夫妻合作社'." 物料号
SELECT-OPTIONS: SO_MATKL FOR MARA-MATKL . "物料组
SELECTION-SCREEN: END OF BLOCK A1.
*屏幕选择定义
*PARAMETERS P_XXXXX
*SELECT-OPTIONS SO_XXXXX
*T1的值统一在INITIALIZATION下定义,不在内文符号下定义.
***********************************************************************************
* INITIALIZATION
***********************************************************************************
INITIALIZATION.
T1 = '选择条件'.
***********************************************************************************
* AT SELECTION-SCREEN
***********************************************************************************
AT SELECTION-SCREEN .
SELECT SINGLE MATNR
INTO MARA-MATNR
FROM MARA
WHERE MATNR EQ P_MATNR .
IF SY-SUBRC NE 0.
MESSAGE '没有查找到相关信息!' TYPE 'I'.
CLEAR p_matnr.
ENDIF.
*MESSAGE 统一用TYPE的方法,不用SE91定义.
***********************************************************************************
* START-OF-SELECTION
***********************************************************************************
START-OF-SELECTION.
*每个程序处理块尽量写明作用和描述
*-------------------------------取库存数量---------------------------*
SELECT A~MATNR A~MATKL A~MEINS B~LABST
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM MARA AS A
INNER JOIN MARD AS B ON A~MATNR EQ B~MATNR
WHERE A~MATNR EQ P_MATNR
AND A~MATKL IN SO_MATKL.
*-------------------------------取物料描述---------------------------*
LOOP AT ITAB.
SELECT SINGLE MAKTX FROM MAKT INTO ITAB-MAKTX
WHERE MATNR EQ ITAB-MATNR
AND SPRAS EQ SY-LANGU.
CALL FUNCTION'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING INPUT = ITAB-MATKL "将要去零的变量输入到此处。
IMPORTING OUTPUT = ITAB-MATKL.
MODIFY ITAB.
CLEAR ITAB.
ENDLOOP.
***********************************************************************************
* TOP-OF-PAGE
***********************************************************************************
TOP-OF-PAGE.
*-------------------------------表头输出---------------------------*
skip.
WRITE AT /45 ' 王石田朴珺夫妻合作社 ' COLOR 1.
WRITE AT /50 ' 仓库库存报表 ' COLOR 1.
skip.
WRITE /1(100) sy-uline .
WRITE: /1 sy-vline,(10) '物料' COLOR 2,
12 sy-vline,(40) '物料描述' COLOR 2,
50 sy-vline,(10) '物料群组' COLOR 2,
65 sy-vline,(20) '数量' COLOR 2,
90 sy-vline,(8) '单位' COLOR 2,
100 sy-vline,
/1(100) sy-uline .
***********************************************************************************
* END-OF-SELECTION
***********************************************************************************
END-OF-SELECTION.
*-------------------------------总表输出---------------------------*
LOOP AT ITAB.
WRITE: /1 sy-vline,(10) ITAB-MATNR NO-GAP CENTERED,
12 sy-vline,(40) ITAB-MAKTX ,
50 sy-vline,(10) ITAB-MATKL RIGHT-JUSTIFIED,
65 sy-vline,(20) ITAB-LABST RIGHT-JUSTIFIED,
90 sy-vline,(25) ITAB-MEINS NO-GAP CENTERED,
100 sy-vline,
/1(100) sy-uline .
ENDLOOP.
***********************************************************************************
* END-OF-PAGE
***********************************************************************************
end-of-page.