ABAP语法示例

1、FIELD-SYMBOLS

FIELD-SYMBOLS是字段符号,类似于FOXPRO的宏,FOXPRO能把一段程序存入一个字符串中,再通过某种方式使该字符串运行。SAP的FIELD-SYMBOLS,把一个结构定义分配给字段符号,通过字段符号来引用结构中的成员。

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
        NAME(30) TYPE C,
        HIGH TYPE P DECIMALS 2,
        WEIGHT TYPE P DECIMALS 2,
     END OF MAN.

FIELD-SYMBOLS <FSA> LIKE MAN.
DATA MAN1 LIKE MAN.
MAN1-NAME = 'JAY'.
MAN1-HIGH = '1.78'.
MAN1-WEIGHT = 140.

ASSIGN MAN1 TO <FSA>.
WRITE:/ <FSA>-NAME,
       <FSA>-HIGH,
       <FSA>-WEIGHT.

输出结果

ABAP语法示例_第1张图片

2、合并字符串

REPORT  YTEST20160527.
DATA:C1(2) TYPE C,
     C2(2) TYPE C,
     C3(2) TYPE C,
     C4(2) TYPE C,
     C5(20) TYPE C,
     C9(2) TYPE C.

C1 = 'AB'.
C2 = 'CD'.
C3 = 'EF'.
C4 = 'GH'.
C9 = '+'.
CONCATENATE C1 C2 C3 C4 INTO C5.
WRITE C5.
CONCATENATE C1 C2 C3 C4 INTO C5 SEPARATED BY C9.
WRITE / C5.

输出结果

ABAP语法示例_第2张图片

3、拆分字符串

REPORT  YTEST20160527.
DATA:C1(2) TYPE C,
     C2(2) TYPE C,
     C3(2) TYPE C,
     C4(2) TYPE C,
     C5(20) TYPE C VALUE '11*22*33*44'.

WRITE C5.
SPLIT C5 AT '*' INTO C1 C2 C3 C4.
WRITE :/ C1,C2,C3,C4.

输出结果

ABAP语法示例_第3张图片

4、内表带有标题行

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
  NAME(20) TYPE C,
  HIGH TYPE P DECIMALS 2,
  WEIGHT TYPE P DECIMALS 2,
  END OF MAN.

DATA: MAN1 LIKE TABLE OF MAN WITH HEADER LINE,
      MAN2 LIKE TABLE OF MAN.

MAN-NAME = 'JAY'.
MAN-HIGH = '1.68'.
MAN-WEIGHT = 120.
APPEND MAN TO MAN1.

MAN-NAME = 'ADOLF'.
MAN-HIGH = '1.69'.
MAN-WEIGHT = 160.
APPEND MAN TO MAN1.

MOVE MAN1[] TO MAN2.

LOOP AT MAN2 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

LOOP AT MAN1.
  WRITE:/ MAN1-NAME,MAN1-HIGH,MAN1-WEIGHT.
ENDLOOP.

输出结果

本例中内表MAN1有表头,内表MAN2无表头,输出结果一样

ABAP语法示例_第4张图片
5、内表排序

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
  NAME(20) TYPE C,
  HIGH TYPE P DECIMALS 2,
  WEIGHT TYPE P DECIMALS 2,
END OF MAN.

DATA: MAN1 LIKE HASHED TABLE OF MAN WITH UNIQUE KEY NAME.

MAN-NAME = 'JAY'.
MAN-HIGH = '1.68'.
MAN-WEIGHT = 120.
INSERT MAN INTO TABLE MAN1.


MAN-NAME = 'ADOLF'.
MAN-HIGH = '1.69'.
MAN-WEIGHT = 100.
INSERT MAN INTO TABLE MAN1.

MAN-NAME = 'BABO'.
MAN-HIGH = '1.70'.
MAN-WEIGHT = 130.
INSERT MAN INTO TABLE MAN1.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

SORT MAN1 BY HIGH WEIGHT DESCENDING.
SKIP.
ULINE.
LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

SORT MAN1 DESCENDING BY HIGH WEIGHT.
SKIP.
ULINE.
LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

输出结果

SORT MAN1 BY HIGH WEIGHT DESCENDING.--------------------只有WEIGHT 字段是降序,其他字段还是默认的升序

SORT MAN1 DESCENDING BY HIGH WEIGHT.--------------------BY后面所有的字段都是降序

多个字段排序,字段在前面的优先排序

ABAP语法示例_第5张图片

6、修改内表数据

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
  NAME(20) TYPE C,
  HIGH TYPE P DECIMALS 2,
  WEIGHT TYPE P DECIMALS 2,
END OF MAN.

DATA: MAN1 LIKE HASHED TABLE OF MAN WITH UNIQUE KEY NAME.

MAN-NAME = 'JAY'.
MAN-HIGH = '1.68'.
MAN-WEIGHT = 120.
INSERT MAN INTO TABLE MAN1.


MAN-NAME = 'ADOLF'.
MAN-HIGH = '1.69'.
MAN-WEIGHT = 100.
INSERT MAN INTO TABLE MAN1.

MAN-NAME = 'BABO'.
MAN-HIGH = '1.70'.
MAN-WEIGHT = 130.
INSERT MAN INTO TABLE MAN1.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

MAN-NAME = 'JAY'.
MAN-HIGH = '2.22'.
MAN-WEIGHT = 180.
MODIFY TABLE MAN1 FROM MAN.

SKIP.
ULINE.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

输出结果

ABAP语法示例_第6张图片
7、删除内表记录

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
  NAME(20) TYPE C,
  HIGH TYPE P DECIMALS 2,
  WEIGHT TYPE P DECIMALS 2,
END OF MAN.

DATA: MAN1 LIKE HASHED TABLE OF MAN WITH UNIQUE KEY NAME.

MAN-NAME = 'JAY'.
MAN-HIGH = '1.68'.
MAN-WEIGHT = 120.
INSERT MAN INTO TABLE MAN1.


MAN-NAME = 'ADOLF'.
MAN-HIGH = '1.69'.
MAN-WEIGHT = 100.
INSERT MAN INTO TABLE MAN1.

MAN-NAME = 'BABO'.
MAN-HIGH = '1.70'.
MAN-WEIGHT = 130.
INSERT MAN INTO TABLE MAN1.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

DELETE MAN1 WHERE NAME = 'BABO'.
SKIP.
ULINE.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

输出结果

8、使用索引插入内表行

REPORT  YTEST20160527.
DATA:BEGIN OF MAN,
  NAME(20) TYPE C,
  HIGH TYPE P DECIMALS 2,
  WEIGHT TYPE P DECIMALS 2,
END OF MAN.

DATA: MAN1 LIKE TABLE OF MAN.

MAN-NAME = 'JAY'.
MAN-HIGH = '1.68'.
MAN-WEIGHT = 120.
INSERT MAN INTO TABLE MAN1.


MAN-NAME = 'ADOLF'.
MAN-HIGH = '1.69'.
MAN-WEIGHT = 100.
INSERT MAN INTO TABLE MAN1.

MAN-NAME = 'BABO'.
MAN-HIGH = '1.70'.
MAN-WEIGHT = 130.
INSERT MAN INTO TABLE MAN1.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

MAN-NAME = 'JIN'.
MAN-HIGH = '1.58'.
MAN-WEIGHT = 110.
INSERT MAN INTO MAN1 INDEX 2.
SKIP.
ULINE.

LOOP AT MAN1 INTO MAN.
  WRITE:/ MAN-NAME,MAN-HIGH,MAN-WEIGHT.
ENDLOOP.

输出结果

ABAP语法示例_第7张图片


你可能感兴趣的:(SAP,语法,示例,abap)