【IT168 技术文章】
在这里我给初学者示范一个实例,讲讲abap开发报表的简单过程。程序的正确与否我们不与讨论,我们只关心这个程序的编写过程。
首先,在任何程序的开始阶段,我们都要确定将会用到的表。我们所讲的这个程序是查询采购订单是否完成的。将会用到以下三个表:EKKO,EKPO,EKET。
程序的开头为:
PROGRAM TEST.
TABLES: EKKO,EKET,EKPO.
TABLES: EKKO,EKET,EKPO.
现在我们已经定义了所要用到的表,接下来的工作就是定义内表和数据。内表是用来存储我们从上述三个表中查询到的数据。相关定义如下:
DATA: BEGIN OF ITAB OCCURS 0,
EBELN LIKE EKKO-EBELN,
MENGE LIKE EKPO-MENGE,
WEMNG LIKE EKET-WEMNG,
END OF ITAB.
EBELN LIKE EKKO-EBELN,
MENGE LIKE EKPO-MENGE,
WEMNG LIKE EKET-WEMNG,
END OF ITAB.
现在我们已经定义完了内表ITAB。接着我们该定义数据了。数据就像C语言里的常量。其相关定义如下:
DATA: MATNR LIKE EKPO-MATNR,
MENGE LIKE EKPO-MENGE,
WEMNG LIKE EKET-WEMNG,
EBELN LIKE EKPO-EBELN.
MENGE LIKE EKPO-MENGE,
WEMNG LIKE EKET-WEMNG,
EBELN LIKE EKPO-EBELN.
到这里我们已经把程序里要用到的数据和内表都定义好了。接着我们要做的工作就是选择屏幕。所谓选择屏幕是指程序运行时,你要从屏幕上输入的内容。比如说一个程序运行时,你输入一个物料号,就可以得到该物料的当前的库存数,那你就要编写一个屏幕以供查询者输入查询的条件。其相关定义如下:
SELECT-OPTIONS: EBELN1 FOR EKKO-EBELN,
BEDAT FOR EKKO-BEDAT,
EKGRP FOR EKKO-EKGRP.
PARAMETERS: R1 RADIOBUTTON GROUP RADI,
R2 RADIOBUTTON GROUP RADI,
R3 RADIOBUTTON GROUP RADI.
BEDAT FOR EKKO-BEDAT,
EKGRP FOR EKKO-EKGRP.
PARAMETERS: R1 RADIOBUTTON GROUP RADI,
R2 RADIOBUTTON GROUP RADI,
R3 RADIOBUTTON GROUP RADI.
现在我们已经完成了程序的前期准备,接着就开始真正的到表里查询我们需要的相关数据了。其代码如下:
SELECT K~EBELN
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM EKKO AS K
WHERE K~EBELN IN EBELN1
AND K~BEDAT IN BEDAT
AND K~EKGRP IN EKGRP.
LOOP AT ITAB.
SELECT SUM( MENGE ) INTO ITAB-MENGE
FROM EKPO
WHERE EBELN EQ ITAB-EBELN.
MODIFY ITAB.
ENDLOOP.
LOOP AT ITAB.
SELECT SUM( WEMNG ) INTO ITAB-WEMNG
FROM EKET
WHERE EBELN EQ ITAB-EBELN.
MODIFY ITAB.
ENDLOOP.
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM EKKO AS K
WHERE K~EBELN IN EBELN1
AND K~BEDAT IN BEDAT
AND K~EKGRP IN EKGRP.
LOOP AT ITAB.
SELECT SUM( MENGE ) INTO ITAB-MENGE
FROM EKPO
WHERE EBELN EQ ITAB-EBELN.
MODIFY ITAB.
ENDLOOP.
LOOP AT ITAB.
SELECT SUM( WEMNG ) INTO ITAB-WEMNG
FROM EKET
WHERE EBELN EQ ITAB-EBELN.
MODIFY ITAB.
ENDLOOP.
然后我们需要定义输出界面。把我们查询到的数据输出时要有一个输出的格式, 这样大家看起来才会方便。代码如下:
TOP-OF-PAGE .
WRITE: /'采购凭证号',22 '物料号码',
47 '采购订单数量',77 '收到货物数量',
95 '完成标志'.
ULINE AT /1(130).
END-OF-PAGE .
START-OF-SELECTION.
WRITE: /'采购凭证号',22 '物料号码',
47 '采购订单数量',77 '收到货物数量',
95 '完成标志'.
ULINE AT /1(130).
END-OF-PAGE .
START-OF-SELECTION.
最后就是把我们查到的数据输出了。其实我举的这个程序到这一步时穿插了一些查询,所以比较长。代码如下:
IF R1 = 'X'.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE > ITAB-WEMNG.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '否'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ELSEIF R2 = 'X'.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE <= ITAB-WEMNG.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '是'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ELSE.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE > ITAB-WEMNG.
WRITE: /2 EBELN, 23 MATNR,41 MENGE,
71 WEMNG,97 '否'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ENDIF.
LOOP AT ITAB.
IF R3 = 'X' AND ITAB-MENGE <= ITAB-WEMNG.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '是'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDSELECT.
ENDIF.
ENDLOOP.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE > ITAB-WEMNG.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '否'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ELSEIF R2 = 'X'.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE <= ITAB-WEMNG.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '是'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ELSE.
LOOP AT ITAB.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
IF ITAB-MENGE > ITAB-WEMNG.
WRITE: /2 EBELN, 23 MATNR,41 MENGE,
71 WEMNG,97 '否'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDIF.
ENDSELECT.
ENDLOOP.
ENDIF.
LOOP AT ITAB.
IF R3 = 'X' AND ITAB-MENGE <= ITAB-WEMNG.
SELECT S~MATNR S~MENGE P~WEMNG S~EBELN
INTO (MATNR, MENGE, WEMNG, EBELN)
FROM EKPO AS S INNER JOIN EKET AS P
ON S~EBELP = P~EBELP AND S~EBELN = P~EBELN
WHERE S~EBELN = ITAB-EBELN.
WRITE: /2 EBELN,23 MATNR,41 MENGE,
71 WEMNG,97 '是'.
ULINE AT /1(130).
CLEAR: EBELN,MATNR,MENGE,WEMNG.
ENDSELECT.
ENDIF.
ENDLOOP.
这时程序就结束了。其实举这个例子是想让大家知道ABAP开发报表的一个常规流程,并不是讲什么技巧之类的,希望能对初学者有点用。抛砖引玉!