ABAP类型详解编程语言

1.T1

*&---------------------------------------------------------------------* 
*& REPORT ZWXT_01 
*&---------------------------------------------------------------------* 
*& 
*&---------------------------------------------------------------------* 
REPORT ZWXT_01. 
 
DATA: 
      GV_CHAR TYPE C LENGTH 10 VALUE 'CHAR TYPE', 
      GV_NUMC TYPE N LENGTH 10 VALUE '123', 
      GV_DEC  TYPE P LENGTH 10 DECIMALS 2 VALUE '3.1495926', 
      GV_INT  TYPE I VALUE 9, 
      GV_DATE1 TYPE D VALUE '20201015', 
      GV_DATE2 TYPE D VALUE '20201009', 
      GV_TIME1 TYPE T VALUE '135055', 
      GV_TIME2 TYPE T VALUE '134050'. 
 
WRITE:/ 'CHAR:', GV_CHAR, 
      / 'NUMC:', GV_NUMC NO-ZERO LEFT-JUSTIFIED, 
      / 'DEC:', GV_DEC, 
      / 'INT:', GV_INT, 
      / 'DATE:', GV_DATE1, 
      / 'TIME:', GV_TIME1. 
 
GV_INT = GV_DATE1 - GV_DATE2. 
WRITE: / 'DATE_DIFF:', GV_INT. 
 
GV_INT = GV_TIME1 - GV_TIME2. 
WRITE: / 'TIME_DIFF:', GV_INT. 
 
WRITE: / 'SUBSTRING:', GV_DATE1+0(4). 
 
* 赋值方法 
 
GV_DATE2 = GV_DATE1 . 
MOVE GV_DATE1 TO GV_DATE2 . 
WRITE GV_DATE1 TO GV_DATE2 . 
 
WRITE GV_DEC  TO GV_CHAR CURRENCY 'KRW' . "韩元会*100 
WRITE: / GV_CHAR . 
 
WRITE GV_NUMC TO GV_CHAR NO-ZERO . 
WRITE: / GV_CHAR . 
CONDENSE GV_CHAR . "CONDENSE去掉CHAR前面的空格 
WRITE: / GV_CHAR . 
 
* 参照全局类型 
DATA: GV_SEX TYPE ZSEX_WX_01 VALUE 'M' , 
      GV_ZUNIT TYPE ZWXSTUDENT_01-ZUNIT . 
WRITE: / GV_SEX . 
 
* 程序中声明数据类型 
TYPES GTY_CHAR TYPE C LENGTH 10 . 
DATA GV_CHAR01 TYPE GTY_CHAR VALUE 'ABCD' . 
WRITE: / GV_CHAR01 . 
 
* 结构体类型的声明 
DATA GS_SCHOOL TYPE ZSCHOOL_WX_STR_01 . 
DATA GS_SCHOOL_02 TYPE ZSCHOOL_WX_STR_01 . 
GS_SCHOOL-ZSCHOOL = '101' . 
GS_SCHOOL-ZSNAME = '清华大学' . 
GS_SCHOOL-ZADD = '五道口' . 
MOVE-CORRESPONDING GS_SCHOOL TO GS_SCHOOL_02 . 
GS_SCHOOL_02-ZADD = '五道口分口' . 
WRITE: / GS_SCHOOL-ZSCHOOL, GS_SCHOOL-ZSNAME, GS_SCHOOL-ZADD . 
WRITE: / GS_SCHOOL_02-ZSCHOOL, GS_SCHOOL_02-ZSNAME, GS_SCHOOL_02-ZADD . 
 
* 结构体类型 
TYPES:  BEGIN OF GTY_SCHOOL , 
        SID     TYPE ZSCHOOL_WX_STR_01 , 
        ZSCHOOL TYPE ZWXSCHOOL_01-ZSCHOOL , 
        ZSNAME  TYPE ZWXSCHOOL_01-ZSNAME , 
        ZADD    TYPE ZWXSCHOOL_01-ZADD , 
        END OF GTY_SCHOOL . 
 
DATA GS_SCHOOL_03 TYPE GTY_SCHOOL . 
GS_SCHOOL_03-SID-ZSCHOOL = '100' . 
MOVE-CORRESPONDING GS_SCHOOL_02 TO GS_SCHOOL_03 . 
GS_SCHOOL_03-ZADD = '五道口分口右侧' . 
WRITE: / GS_SCHOOL_03-SID-ZSCHOOL, GS_SCHOOL_03-ZSCHOOL, GS_SCHOOL_03-ZSNAME, GS_SCHOOL_03-ZADD . 
 
TYPES: BEGIN OF GTY_SCHOOL_02 . 
       INCLUDE STRUCTURE ZWXSCHOOL_01 . 
TYPES: FLAG TYPE C LENGTH 1 , 
       END OF GTY_SCHOOL_02 . 
 
DATA GS_SCHOOL_04 TYPE GTY_SCHOOL_02 . 
MOVE GS_SCHOOL_03-SID-ZSCHOOL TO GS_SCHOOL_04-ZSCHOOL . 
WRITE: / GS_SCHOOL_04-ZSCHOOL . 
 
* 内表 INTERNAL TABLE 
* 参照结构 
"TYPES GTY_SCHOOL_T TYPE TABLE OF ZSCHOOL_WX_STR_01 . 
TYPES GTY_SCHOOL_T2 TYPE ZSCHOOL_WX_TTY_01 . 
 
* 声明内表 
DATA GT_SCHOOL TYPE TABLE OF ZSCHOOL_WX_STR_01. 
DATA GT_SCHOOL_02 TYPE TABLE OF ZSCHOOL_WX_STR_01 INITIAL SIZE 0 . 
 
GS_SCHOOL-ZSCHOOL = '105' . 
GS_SCHOOL-ZSNAME  = '常熟理工' . 
GS_SCHOOL-ZADD    = '江苏常熟'  . 
APPEND GS_SCHOOL TO GT_SCHOOL . 
 
CLEAR GS_SCHOOL . 
GS_SCHOOL-ZSCHOOL = '106' . 
GS_SCHOOL-ZSNAME  = '常熟理工' . 
GS_SCHOOL-ZADD    = '江苏常熟'  . 
APPEND GS_SCHOOL TO GT_SCHOOL . 
 
READ TABLE GT_SCHOOL INTO GS_SCHOOL INDEX 1 . 
IF SY-SUBRC = 0. 
  GS_SCHOOL-ZSCHOOL = '108' . 
  MODIFY GT_SCHOOL FROM GS_SCHOOL INDEX 1. 
ENDIF. 
 
LOOP AT GT_SCHOOL INTO GS_SCHOOL. 
 
IF GS_SCHOOL-ZSCHOOL = '106'. 
  GS_SCHOOL-ZSCHOOL = '108' . 
  MODIFY GT_SCHOOL FROM GS_SCHOOL. 
ENDIF. 
 
ENDLOOP. 
 
 
*.. 
 
DATA: BEGIN OF GTY_SCHOOL_A, 
       SID     TYPE GTY_SCHOOL_T2 , 
       ZSCHOOL TYPE ZWXSCHOOL_01-ZSCHOOL , 
       ZSNAME  TYPE ZWXSCHOOL_01-ZSNAME , 
       ZADD    TYPE ZWXSCHOOL_01-ZADD , 
      END OF GTY_SCHOOL_A . 
 
* 内表赋值 
GTY_SCHOOL_A-SID[] = GT_SCHOOL[] . 
 
* 带有表头行的内表 
DATA GT_SCHOOL_H TYPE TABLE OF ZSCHOOL_WX_STR_01 WITH HEADER LINE . 
 
GT_SCHOOL_H-ZSCHOOL = '109' . 
GT_SCHOOL_H-ZSNAME  = '江南大学'  . 
GT_SCHOOL_H-ZADD    = '江苏无锡'  . 
* 因为GT_SCHOOL_H带有表头,因此不需要再申明工作区,因此GT_SCHOOL_H是表头行,GT_SCHOOL_H[]是内表 
APPEND GT_SCHOOL_H TO GT_SCHOOL . 
APPEND GT_SCHOOL_H . 
LOOP AT GT_SCHOOL INTO GS_SCHOOL. 
  WRITE: / GS_SCHOOL. 
ENDLOOP. 
SKIP. 
 
LOOP AT GT_SCHOOL_H . 
  WRITE: / GT_SCHOOL_H. 
ENDLOOP. 
 
* 常量,常量声明后必须赋值 
CONSTANTS C_CHAR TYPE C LENGTH 5 VALUE 'C_CHAR'. 
 
* 系统自带常用参数 
* sy-subrc:返回代码值 0代表成功 
* sy-uname: 返回用户名 
* sy-tcode: 返回当前事务代码 
* sy-datum: 当前日期 
* sy-uzeit: 当前时间 
* sy-repid: 当前程序名 
SKIP. 
 
WRITE: / SY-SUBRC, 
       / SY-UNAME, 
       / SY-TCODE, 
       / SY-DATUM, 
       / SY-UZEIT, 
       / SY-REPID. 
 
SKIP. 
DATA: GV_TEST TYPE C . 
IF GV_TEST IS INITIAL. 
  WRITE: / 'GV_TEST IS EMPTY' . 
ENDIF. 
 
* 内表分类 
* 1.标准表 2.排序表 3.哈希表 4.一般类型表 5.任意表。 
DATA GT_SCHOOL_STANDARD TYPE STANDARD TABLE OF ZSCHOOL_WX_STR_01 . 
DATA GT_SCHOOL_SORTED TYPE SORTED TABLE OF ZSCHOOL_WX_STR_01 WITH NON-UNIQUE KEY ZSCHOOL. 
DATA GT_SCHOOL_HASHED TYPE HASHED TABLE OF ZSCHOOL_WX_STR_01 WITH UNIQUE KEY ZSCHOOL ZSNAME. 
 
* 内表索引:SY-TABIX 
 
CONSTANTS C_CON TYPE C LENGTH 5 VALUE 'SAP'. 
DATA GS_SCHOOL_S TYPE ZWXSCHOOL_01. 
DATA GT_SCHOOL_T TYPE STANDARD TABLE OF ZWXSCHOOL_01. 
 
DATA GS_STUDENT_S TYPE ZWXSTUDENT_01. 
DATA GT_STUDENT_T TYPE STANDARD TABLE OF ZWXSCHOOL_01 WITH HEADER LINE. 
 
TYPES: BEGIN OF GTY_INFO_S, 
        ZCODE TYPE  ZWXSTUDENT_01-ZCODE, 
        ZNAME TYPE  ZWXSTUDENT_01-ZNAME, 
        SEX   TYPE  ZWXSTUDENT_01-SEX, 
        SZSCHOOL TYPE ZWXSCHOOL_01-ZSCHOOL, 
        ZSNAME   TYPE ZWXSCHOOL_01-ZSNAME, 
       END OF GTY_INFO_S. 
 
DATA GT_TOTAL TYPE STANDARD TABLE OF GTY_INFO_S. 
DATA GS_TOTAL TYPE GTY_INFO_S.

2.T2-内表操作

*&---------------------------------------------------------------------* 
*& REPORT ZWXT_02 
*&---------------------------------------------------------------------* 
*& 
*&---------------------------------------------------------------------* 
REPORT zwxt_02. 
 
DATA gs_school TYPE zschool_wx_str_01. 
DATA gt_school TYPE STANDARD TABLE OF zschool_wx_str_01 WITH HEADER LINE. 
DATA gt_school_copy TYPE STANDARD TABLE OF zschool_wx_str_01. 
DATA gt_school_sorted TYPE SORTED TABLE OF zschool_wx_str_01 WITH UNIQUE KEY zschool WITH HEADER LINE. 
DATA gt_school_hashed TYPE HASHED TABLE OF zschool_wx_str_01 WITH UNIQUE KEY zschool WITH HEADER LINE. 
* 内表的操作 
* 内表数据的增加 APPEND,只建议在标准表使用 
gs_school-zschool = '101'. 
gs_school-zsname  = '清华大学'. 
gs_school-zadd    = '五道口'. 
* 使用自定义工作区插入 
APPEND gs_school TO gt_school. 
APPEND gs_school TO gt_school_copy. 
 
gt_school-zschool = '102'. 
gt_school-zsname  = '北京大学'. 
gt_school-zadd    = '朝阳区'. 
* 使用表头自带工作区插入 
APPEND gt_school. 
APPEND gt_school TO gt_school_copy. 
* 插入空行 
APPEND INITIAL LINE TO gt_school. 
* 将COPY表里的前两行插入GT_SCHOOL表里,如果不加FROM和TO则将所有的数据插入GT_SCHOOL 
APPEND LINES OF gt_school_copy FROM 1 TO 2 TO gt_school. 
 
 
 
* 内表数据的插入 INSERT,可以插入到指定位置,不能插哈希表 
CLEAR gs_school. 
gs_school-zschool = '103'. 
gs_school-zsname  = '浙江大学'. 
gs_school-zadd    = '浙江'. 
INSERT gs_school INTO gt_school INDEX 5. 
 
CLEAR gs_school. 
gs_school-zschool = '104'. 
gs_school-zsname  = '湖南大学'. 
gs_school-zadd    = '长沙'. 
INSERT gs_school INTO TABLE gt_school_hashed. 
gs_school-zschool = '102'. 
INSERT gs_school INTO TABLE gt_school_hashed. 
 
INSERT LINES OF gt_school_copy FROM 1 TO 2 INTO gt_school INDEX 3. 
 
LOOP AT gt_school. 
  WRITE: / sy-tabix, gt_school. 
ENDLOOP. 
ULINE. 
LOOP AT gt_school_hashed. 
  WRITE: / sy-tabix, gt_school_hashed. 
ENDLOOP. 
 
* 内表数据插入 COLLECT 将关键字段和内表的字段比较,如果相同的话不增加新行,将数值字段和内表相关行的字段加起来将进行更新 
* 如果不存在,则插入新的一行 
DATA: BEGIN OF gt_collect OCCURS 0, 
        name(10) TYPE c, 
        money TYPE i, 
      END OF gt_collect. 
gt_collect-name = '甲'. 
gt_collect-money = 10. 
COLLECT gt_collect. 
gt_collect-name = '甲'. 
gt_collect-money = 20. 
COLLECT gt_collect. 
gt_collect-name = '甲'. 
gt_collect-money = 30. 
COLLECT gt_collect. 
gt_collect-name = '乙'. 
gt_collect-money = 10. 
COLLECT gt_collect. 
gt_collect-name = '乙'. 
gt_collect-money = 20. 
COLLECT gt_collect. 
ULINE. 
LOOP AT gt_collect. 
  WRITE: / gt_collect-name,gt_collect-money. 
ENDLOOP. 
 
* 内表数据的修改 MODIFY 
gs_school-zschool = '110'. 
gs_school-zsname  = '江苏科技大学'. 
MODIFY gt_school FROM gs_school INDEX 1. 
* 不加索引需要写MODIFY TABLE 
MODIFY TABLE gt_school_hashed FROM gs_school. 
ULINE. 
LOOP AT gt_school into gs_school. 
   IF gs_school-zsname cn '大学'. 
     REPLACE '大学' with '小学' into gs_school-zsname. 
     modify gt_school from gs_school. 
     WRITE:/ gs_school. 
   ENDIF. 
ENDLOOP. 
ULINE. 
 
WRITE:/ '哈希表排序前:'. 
LOOP AT gt_school_hashed into gs_school. 
   IF gs_school-zsname cn '大学'. 
     CONCATENATE '中国' gs_school-zsname into gs_school-zsname. 
* 哈希表没有索引 因此要加TABLE 否则报错 transporting指明只修改某些字段 
     MODIFY TABLE gt_school_hashed from gs_school transporting zsname zadd. 
   ENDIF. 
ENDLOOP. 
 
LOOP AT gt_school_hashed into gs_school. 
  WRITE: / gs_school. 
ENDLOOP. 
ULINE. 
 
* 二分查找前先排序 
CLEAR gs_school. 
* 默认升序排列 
SORT gt_school BY zschool ASCENDING ."DESCENDING. 
READ TABLE gt_school INTO gs_school WITH KEY ZSNAME = '北京小学' BINARY SEARCH. 
WRITE: / gs_school. 
 
READ TABLE gt_school INTO gs_school WITH KEY ZSNAME = '北京小学1' TRANSPORTING NO FIELDS. 
IF SY-SUBRC = 0. 
  WRITE: / '查询成功'. 
ELSE. 
  WRITE: / '查询失败'. 
ENDIF. 
ULINE. 
 
* 在loop后加where 条件 
LOOP AT gt_school INTO gs_school WHERE ZSNAME is not INITIAL and zadd = '长沙'. 
  WRITE: / gs_school. 
ENDLOOP. 
ULINE. 
 
*哈希表排序 
SORT gt_school_hashed BY ZSCHOOL. 
WRITE:/ '哈希表排序后:'. 
LOOP AT gt_school_hashed. 
  WRITE: / gt_school_hashed. 
ENDLOOP. 
ULINE. 
 
CLEAR gs_school. 
* 哈希表/排序表的读取 
READ TABLE gt_school_hashed INTO gs_school WITH TABLE KEY zschool = '104'. 
WRITE: / gs_school. 
ULINE. 
 
* 内表数据的删除 DELETE 
DELETE gt_school INDEX 1. 
DELETE TABLE gt_school_hashed FROM gs_school. 
 
LOOP AT gt_school_hashed. 
  WRITE:/ gt_school_hashed. 
ENDLOOP. 
ULINE. 
 
* 删除临近的重复的行 
SORT gt_school BY ZSCHOOL ZSNAME ZADD. 
DELETE ADJACENT DUPLICATES FROM gt_school COMPARING zschool. 
CLEAR gs_school. 
LOOP AT gt_school INTO gs_school. 
  WRITE:/ gs_school. 
ENDLOOP. 
ULINE. 
 
* 如果有表头行清空表头 
CLEAR GS_SCHOOL. 
CLEAR gt_school_sorted[]. 
 
* 如果有表头行清空表体 
REFRESH gt_school_hashed. 
 
* 释放内存空间 
FREE gt_school_hashed.

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/18017.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论