SAP ABAP游标的使用(示例)

这个例子是我在《ABAP HANDBOOK》这个书里看到的,从示例学习ABAP,比看枯燥的文字更能理解和掌握。与NEWBIE一起共勉。

*&---------------------------------------------------------------------*
*& REPORT  Z_WUWEI_0019
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  Z_WUWEI_0019.

*/DECLARING CURSORS-C1 AND C2

DATA: C1 TYPE CURSOR,
      C2 TYPE CURSOR.

DATA: WORK_AREA1 TYPE MARA,
      WORK_AREA2 TYPE MARA.

DATA: FLAG1(1) TYPE C,
      FLAG2(1) TYPE C.

*/OPENING CURSORS-C1 AND C2
OPEN CURSOR: C1 FOR SELECT MATNR  "物料号
                           ERSDA  "创建日期
                           ERNAM  "创建对象的人员名称
                        FROM MARA  "常规物料数据
                        WHERE ERNAM = 'ZHANGSHEN',
             C2 FOR SELECT LAEDA  "上次更改的日期
                           AENAM  "更改对象用户的名称
                        FROM MARA
                        WHERE ERNAM = 'ZHANGSHEN'.
DO.
  IF FLAG1 NE 'X'.
*/USING THE C1 CURSOR TO FETCH THE DATA
    FETCH NEXT CURSOR C1 INTO CORRESPONDING FIELDS OF WORK_AREA1.

    IF SY-SUBRC <> 0.
*/CLOSING THE C1 CURSOR
      CLOSE CURSOR C1.
      FLAG1 = 'X'.
    ELSE.
      WRITE: / '物料号:' COLOR 7 , WORK_AREA1-MATNR,'创建日期:' COLOR 6 , WORK_AREA1-ERSDA ,'创建对象的人员名称:' COLOR 5,WORK_AREA1-ERNAM .
    ENDIF.
  ENDIF.

  IF FLAG2 NE 'X'.
*/USING THE C2 CURSOR TO FETCH THE DATA
    FETCH NEXT CURSOR C2 INTO CORRESPONDING FIELDS OF WORK_AREA2.
    IF SY-SUBRC <> 0.
*/CLOSING THE C2 CURSOR
      CLOSE CURSOR C2.
      FLAG2 = 'X'.
    ELSE.
      WRITE: / '上次更改的日期:' COLOR 4 , WORK_AREA2-LAEDA ,'更改对象用户的名称:' COLOR 3 , WORK_AREA2-AENAM.
    ENDIF.
  ENDIF.
  IF FLAG1 = 'X' AND FLAG2 = 'X'.
    EXIT.
  ENDIF.
ENDDO.


你可能感兴趣的:(SAP ABAP游标的使用(示例))