Abap内表
什么是内表:内表是内存中建立的一个临时表,你可以在程序运行时对表中的数据进行,插入,修改,删除等操作,程序跑完了,就会被释放。
定义类型:通过types开头定义
TYPES: BEGIN OF line,
field1 TYPE i,
field2 TYPE i,
END OF line.
参考类型定义结构、工作区域、变量:定义结构通过data开头定义
DATA: WA _ITAB TYPE(LIKE) line. “ 声明一个内表工作区
DATA: ITAB TYPE(LIKE) STANDARD TABLE OF line .
参考工作区域定义内表,
Data itab like table of wa_itab
参考内表定义工作区,
Data wa_itab like lines of itab
填充内表行
append <wa> to <itab> “ 不带表头行的填充
append <itab> “ 带隐式表头行的填充
插入内表行
insert <wa> into <itab> [INDEX idx] “
insert <itab> [INDEX idx ] “ 隐式表头行插入内表
-- 如果没有指定INDEX ,则默认插入到内表最后一行
读取内表行
read <itab> into <wa> [INDEX idx]
read <itab> [INDEX IDX]
修改内表行
modify <itab> from <wa> [INDEX idx]
modify <itab> [INDEX idx]
-- read itab index 3 .
-- itab-XX = ‘xxx’ .
-- modify itab index 4 .
删除内表行
delete <itab> [INDEX idx] .
-- 带表头行和不带表头行语法一致 。
内表循环
Loop at <itab> into <wa> .
<statement block>
endloop. “ 带表头行的内表循环操作
Loop at <itab> .
<statement block>
endloop. “ 不带表头行内表操作
-- 循环体的MODIFY,DELETE等语句不必指定INDEX项,系统默认处理当前行 .
-- 如果不需要读取所有的内表行,可以使用WHERE选项进行限制
-- LOOP AT <itab> [WHERE <conditions>]
清空内表
clear <itab> . “清空不带表头行内表
clear<itab>[] . “清空带表头行内表
内表排序
SORT <itab> [ASCENDING | DESCENDING] [AS TEXT]
--ASCENDING和DESCENDING指定升序还是降序排列,如果不指定,缺省排序方式是升序
删除重复行
DELETE ADJACENT(紧接着的;相邻) DUPLICATES(副本) FROM <itab> [COMPARING <comp>].
-- 删除重复行之前须对内表进行排序
将内表中部分或全部的数据行整体插入另一内表
INSERT LINES OF <itab1> [FROM n1] [TO n2] INTO [TABLE] <itab2> [INDEX <idx>].
-- insert lines of itab1 from 1 to 100 into itab2 . “ 将内表1前100行数据附加到内表2 .
-- 两个内表必须具有相同的或可转换的行结构
将内表中部分或全部的数据行整体填充到另一内表
APPEND LINES OF <itab1> [FROM n1] [TO n2] TO <itab2>.
按照条件或者索引删除一组选定行
DELETE <itab> [FROM n1] [TO n2] [WHERE <condition>].
-- delete itab1 from 1 to 100 where age >30 . "删除内表前100行中年龄大于30 的记录 。
整体复制内表,目标内表原有内容被覆盖
MOVE <itab1> TO <itab2> 不带表头行的内表之间进行复制
MOVE <itab1>[] TO <itab2>[]. 带表头行的内表之间进行复制
MOVE <itab1> TO <itab2>[]. 不带表头行的内表复制到带表头行的内表
MOVE-CORRESPONDING <itab1> TO <itab2> 复制内表1中与内表2具有相同结构的字段进行复制,同样适用于工作区
EX.
- 构造一个内表BOOK,字段有BOOKNO,BOOKNAME,ZUOYE,PRICE.
- 输入几组数据:
BOOKNO |
BOOKNAME |
ZUOYE |
PRICE |
1 |
TEST |
MARK |
55 |
2 |
ERRO |
WRITE |
43 |
3 |
PRO |
BACKER |
67 |
4 |
中国历史 |
QINCHAO |
88 |
3、插入一个新的数据在第三行,每一列数据分别是 ‘5’,’insert’,’insert’,’36’
4、删除第4行数据
5、把数据复制给一个新的内表BOOK1
6、按照价格排序BOOK
7、修改第2条数据,把PRICE改为15.
DATA:BEGIN OF BOOK OCCURS 0 , BOOKNO(10) TYPE C, BOOKNAME(10) TYPE C, ZUOYE(10) TYPE C, PRICE(2) TYPE N, END OF BOOK. BOOK-BOOKNO = '1'. BOOK-BOOKNAME = 'TEST'. BOOK-ZUOYE = 'MARK'. BOOK-PRICE = '55'. APPEND BOOK. BOOK-BOOKNO = '2'. BOOK-BOOKNAME = 'ERROR'. BOOK-ZUOYE = 'WRITE'. BOOK-PRICE = '43'. APPEND BOOK. BOOK-BOOKNO = '3'. BOOK-BOOKNAME = 'PRO'. BOOK-ZUOYE = 'BAKER'. BOOK-PRICE = '67'. APPEND BOOK. BOOK-BOOKNO = '4'. BOOK-BOOKNAME = '中国历史'. BOOK-ZUOYE = 'QINCHAO'. BOOK-PRICE = '88'. APPEND BOOK. LOOP AT BOOK. WRITE:/ BOOK. ENDLOOP. WRITE: SY-ULINE. BOOK-BOOKNO = '5'. BOOK-BOOKNAME = 'insert'. BOOK-ZUOYE = 'insert'. BOOK-PRICE = '36'. INSERT BOOK INDEX 3. LOOP AT BOOK. WRITE:/ BOOK. ENDLOOP. WRITE:SY-ULINE. DELETE BOOK INDEX 4. LOOP AT BOOK. WRITE:/ BOOK. ENDLOOP. WRITE:SY-ULINE. DATA:BOOK1 LIKE TABLE OF BOOK WITH HEADER LINE. MOVE BOOK[] TO BOOK1[]. LOOP AT BOOK1. WRITE:/ BOOK1. ENDLOOP. WRITE:SY-ULINE. clear book1. book1-PRICE = '15'. modify BOOK1 INDEX 2 TRANSPORTING price. LOOP AT BOOK1. WRITE:/ BOOK1. ENDLOOP. WRITE:sy-uline. sort book1 by price . LOOP AT BOOK1. WRITE:/ BOOK1. ENDLOOP.