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.

输出结果




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jay_1989/article/details/51536381
文章标签: sap abap 语法 示例
个人分类: ABAP
想对作者说点什么? 我来说一句

从实例学sap abap编程(完整版)

2012年04月19日 27.93MB 下载

ABAP开发实例

2011年12月25日 1.48MB 下载

没有更多推荐了,返回首页

不良信息举报

ABAP语法示例

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭