SAP abap内表分类与增删改查操作
1.内表的分类
1.1.标准表 (standard table )
系统为该表每一行生成一个院级索引.填表是可以将数据附加在现有行之后, 也可以插入到指定的位置,程序对内表的寻址可以通过关键字或者索引进行.在对表进行行插入, 删除等操作, 各行数据在内表的位置不变,系统仅重新排列个数据行的索引值. DATA:ITAB TYPE TABLE OF LINE_TYPE.
讯享网
1.2排序表(sorterd table)
讯享网也具有一个逻辑索引,不同的是排序表总是按照其表关键字升序排序后再进行存储,其访问方式与标准表相同 DATA:ITAB TYPE SORTED TABLE OF LINE_type with unique non-unique key keyname.
1.3.哈希表(hashed table)
没有索引,只能通过关键字来访问,系统用哈希算法来管理表中的数据 data:itab type hashed table of linetype with unique key keyname.
例:
讯享网标准表: data: gt_table_t type standard table of gty_table. 排序表: data:gt_table_s type sorted table of gty_table with non-unique key name. 哈希表: data: gt_table_h type hashed table of gty_table with non-unique key name.
1.4 index table / any tablev
可以定义数据类型,但不能生成数据对象,数据对象一般是在程序中动态指定的, 一般在perform程序块或者是fieldsymbols里使用.
clear 清空
2.表头行
- 在创建内表的同时可以使用选线 with header line 隐式的定义一个工作表头行,也称作内表工作区
-
讯享网
data: itab type table_type with header line - 用以上语句创建内表的同时就隐含的创建一个内表的工作区
2.1内表的引用方法
itab[]
2.2工作区的引用方法
讯享网itab data: gt_table2 type table of gty_tble initial size 10 with header line ."定义表头行的内表 CLEAR : GT_TABLR2, GT_TABLE2[]
3.内表数据的增加
3.1追加 - append
append 相当于在内表的最后一行追加,他只能使用在索引表中而且最好是标准表,而标准表示abap编程中最常用的表.
哈希表不可使用append,排序表不建议使用append.
讯享网append [line | initialline] to itab. append lines of itab1 [from n1 ] [to n2] to itab2. 向表中添加数据 append append gs_score to gt_score. 从结构体 append gt_scprt 从表头行 append lines of gt_score form 1 to 3 to gt_score2.
*说明: line:结构 itab:内表 idx: 索引值
3.2插入 - insert
3.2.1将行插入到内表中指定的位置,需要使用insert语句. 3.2.2对于索引表,可以指定某行的索引,则新行将插入到该索引所代表的的行之前; 3.3.3对于哈希表,不能指定行的索引,系统会按照关键字将新行插入其特殊的位置. 3.2.4给内表插入行可以为单行,也可以为多行,甚至整张表. 3.2.5按照索引值插入(只适用于索引表) insert line into itab index idx 3.2.6按照关键字插入(可用于所有表) insert line into table table 3.2.7多行插入 insert lines of itab1 [from n1][to n2] into [table] itab2 [index idx]. 3.2.8示例 INSERT gs_score INTO gt_score INDEX 1. INSERT gs_score INTO TABLE gt_score . INSERT LINES OF gt_scpre FROM 1 TO 3 INTO TABLE gt_score
3.3聚集附加 - collect
讯享网作用:将工作区里的关键字段值跟内表里的字段值比较, 如果相同的话不在内表里追加行, 而是将工作区里的数值字段值跟内表里的相关行的字段值累加, 然后将工作区里的数字值更新到内表中的相关行里; 如果比较后没有在内表中找到相关行就将工作区里的内容添加到内表中. collect line into itab.
4.内表数据的修改
4.1修改 - modify
4.1根据索引更改内表的语法: modify:itab [from wa] [index idx] transporting f1 f2....].
4.2使用关键字来更改表行可以应用所有类型的内表,语法:
讯享网modify table itab from wa[transporting f1 f2 ...].
4.3示例:
修改内表中的数据 modify modify gt_score from gs_score index 3 transporting name. modify table gt_score from gs_score. *内表循环 loop loop at gt_score into gs_score. if gs_score-name = 'a'. gs_score-sex = 'm'. modify gt_score from gs_scpre. endif. write: / gt_scpre,gs_score-name. endloop.
5.表内容数据的读取
5.1 读取 - read
讯享网对于索引表可以利用索引读取单行 read table [into wa | assigning <fs> ] index idx. 可以通过关键字读取任何类型的内表 read table itab with [table]key k1 = f1 ..[into wa] | assigning <fs>] index idx. 使用关键字table 要求指定的key字段必须都是关键字段,并且把所有的关键字段都列出来,是哈希表读取时比较适合的语句 不使用table关键字,指定的key不必都是关键字也无需把所有的关键字段都列出来,是索引表读取时比较适合的语句. 对于索引表的读取可以使用binary search语句加快读取速度. binary search叫做二分法搜索,可以成几倍的加过搜索速度. 条件:1.内表必须是索引表.2.内表必须是已经按照搜索的关键字排序.
6.内表数据的删除
6.1 删除 - delete
根据索引删除内表的语法 delete itab [from wa ] [index idx]. 使用表关键字来删除内表行可以应用所有类型的表 delete table itab from wa 删除邻近的重复行 sort itab by field1 field2 delete adjacent duplicates from itab [comparing field1 field2].
6.2清空表内容
讯享网clear :itab,itab[] . refresh : itab.
6.3 示例
*删除内表的数据 delete delete gt_score index 1. delete table gt_score from gs_score. sort gt_score by name. delete adjacent duplicates from gt_socre comparing name.
7.内表的数据的增加
open SQL - select
从数据库表中向内表/结构中取数 : select
7.1取多条数据
讯享网select * from database table into[corresponding] table itab
7.2示例
*取出字段的值依次序放入内表中的字段 select * from zhq_score_01 into table gt_score. *取出字段的值对应放入内表中的字段 select * from zhq_score_01 into corresponding fields of table gt_score. *取出所有的字段对应放入内表中的字段 select * from zhq_score_01 into corresponding field of table gt_score.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/112303.html