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.

输出结果

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.

输出结果

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.

输出结果

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无表头,输出结果一样


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后面所有的字段都是降序

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

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.

输出结果


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.

输出结果




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值