ABAP之内表

内表: 可以在程序中使用定义的表, 它是ABAP语言区别与其他语言最显著的特点之一。

   它只存在于内存中与DB无关,定义时不会占用内存,向内表中追加数据时其占用的内存也会相应增加。

   内表类似于JAVA语言中的LIST<MAP>, 多行数据,每行数据有不同的字段的这种数据结构。

内表的创建(有多种方式创建):

  1. 参照表类型创建内表。  表类型可以是局部Types(程序中声明的Table Type) ,也可以是在全局建的Table Type。  

  eg: data itab type gty_type.

  2. 也可以参照数据库表定义内表。data itab type standard table of t001.

 

**    其中abap中的数据库不是真正的数据库,而是通过sap软件封装过的数据库,sap软件可以去连接oracle、mysql等,

  但是abap语言用的数据库指的是封装后数据库。

 

  接下来引入一个费解的概念: 工作区(表头)Header Line.

  形象的说:内表表体相当于List<Map> 表头就相当于Map。  任何一行记录获取出来都需要接收体,即就是工作区。

 

  不带表头和带表头的内表在使用上有一些区分

eg:  loop itab into wa.    loop itab.

   read table itab into wa.     read table itab.

   modify table itab from.    modify table itab.

  delete table itab from wa.   delete table itab.

  append wa to itab.      append itab.

 

  内表有哪些类型呢:

  1. 标准表 属于索引表可以通过index进行访问

  2. 排序表 属于索引表可以通过index进行访问

  3. 哈希表  属于非索引表 只能通过关键字访问。

  访问大数据量的清空下, 访问速度排序 哈希表速度大于 排序表 大于标准表

  内表赋值可以通过 move itab1 to itab2. (只会对表头进行赋值)。

  move-corresponding itab1[] to itab2[].

  可以完成两个内表同名字段的赋值。

 

  CLEAR、REFRESH、FREE的区别

  clear:   可以清空表数据,但是不释放已分配的内存空间。如果是带表头的内表 只能清空表头,为了清空内表需要使用clear itab[].

  refresh: 可以清空内表数据,带表头的内表,只能清空内表数据,但是不释放已经分配的内存空间。

  free:  清空内表数据和释放已经分配的内存空间,如果带表头的内表,无法清空表头。经常和clear结合使用。

 

  sort 对内表进行排序,  sort itab 用内表自带的关键字进行排序。

  指定字段排序写法 sort itab ascending by f1 asce.  注意 排序表不能进行排序。

 

  describe 用来确认内表属性

  describe· table itab lines lv_line.  获取内表的行数

  describe· table itab occurs lv_init.  获取内表的初始是大小

  describe· table itab kind lv_kind.  获取内表的类型 T标准表 S排序表 H哈希表。

 

  内表的增删改查操作:

  增加: insert append collect

    eg:  insert line into table itab." 增加一条数据

      insert lines of itab1 into table itab2 “增加 itab1多条数据至itab2表中。

      append追加数据 append line to itab.      append lines of itab1 to itab2.    append lines of itab1 to itab2

      哈希表不能使用append,排序表使用append需要排序好的顺序进行追加,负责发生Dump Error.

 

  删除: delete

  利用关键字进行删除, delete table itab from wa. 利用索引删除 delete itab index idx.

  删除表中重复的数据 ADJACENT DUPLICATES。  使用前需要先进行排序sort,  才能得到预期的结果。如果不适应comparing 语句会删除关键字重复的数据。

  sort itab by f1.

  delete adjacent duplicates from itab comparing f1.

 

  修改: modify

  利用关键字进行修改 modify table itab from wa [ transporting f1 ]

  利用索引进行修改  modify itab from wa index idx.

 

.  查询: read

  利用关键字读取内表数据  read table itab with table key into result.

  利用索引读取内表数据  read table itab index idx into result.

 

  个人对内表的增删改查和openSQL的增删改查难以分清.

 

 

 

 

  

 

posted @ 2019-12-04 14:09  姓蜀名黍  阅读(799)  评论(0编辑  收藏  举报