🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# SAP ABAP 内部表:创建,读取,填充,复制&删除 > 原文: [https://www.guru99.com/all-about-sap-internal-tables.html](https://www.guru99.com/all-about-sap-internal-tables.html) ## 什么是内部表? **内部表**用于从固定结构中获取数据,以便在 ABAP 中动态使用。 内部表中的每一行都具有相同的字段结构。 内部表的主要用途是存储和格式化程序中数据库表中的数据。 在本教程中,您将学习: * [什么是内部表?](#1) * [什么是工作区?](#2) * [内部表和工作区之间的区别?](#3) * [内部表的类型](#4) * [创建内部表](#5) * [填充内部表](#6) * [复制内部表](#7) * [读取内部](#8) * [删除内部表](#9) ## 什么是工作区? 工作区是单行数据。 它们应具有与任何内部表相同的格式。 它用于一次处理一行内部表中的数据。 ## 内部表与工作区之间的区别? 图片说了一千个字:-) ![SAP ABAP Internal Table: Create, Read, Populate, Copy & Delete](https://img.kancloud.cn/9b/ab/9babcd6e1e7bd5c45597b813225e3e73_413x273.png "sap-internal-table") ## 内部表的类型 内部表有两种类型。 1. 带 HEADER 行的内部表 2. 没有 HEADER 行的内部表。 **内部表带有标题行** * 系统在此处自动创建工作区。 * 工作区与内部表具有相同的数据类型。 * 该工作区称为 HEADER 行。 * 在这里,所有更改或对表内容的任何操作均已完成。 结果,记录可以直接插入表中,也可以直接从内部表访问。 **内部表不带标题行**: * 这里没有与表格关联的工作区。 * 当我们需要访问此类表时,应明确指定工作区。 * 因此,这些表不能直接访问。 ## 创建内部表 有多种创建内部表的方法。 让我们一一看一下- **1.通过使用类型语句** 现在让我们使用 TYPE 创建一个内部表 *itab* 声明。 语法是- ``` Types : begin of line, column1 type I, column2 type I, end of line. ``` 例: ``` TYPES : begin of line, empno type I, empname(20) type c , end of line. ``` TYPES 语句按定义创建结构*行*。 要实际创建内部表 *Itab* ,请使用以下命令- ``` Data itab type line occurs 10. ``` 使用 line 的结构创建内部表 *itab* 。除了声明内部表的结构外,OCCURS 子句还定义了在主存储器中维护多少个表条目(在本例中为 10)。 多余的记录会写到分页区域,并会影响性能 **2.通过引用另一个表** ,您可以通过引用现有表来创建内部表。 现有表可以是标准 SAP 表,Z 表或另一个内部表。 语法- ``` Data <f> <type> [with header line]. ``` 例- ``` DATA itab TYPE line OCCURS 10 with header line. ``` 在此,创建带有标题行的类型行的内部表 *itab* 。 请注意,“带有标题行”是可选的 **3.通过引用现有结构** 语法- ``` Data <f> LIKE <struct> occurs n [with header line]. ``` Example- ``` DATA itab LIKE sline OCCURS 10. ``` 这里创建了一个表 *itab* ,其结构与行 **相同。4.通过创建新结构** ,让我们创建一个 内部表具有我们自己的结构。 在这里,表是用标题行创建的,默认为为**。 语法-** ``` Data : Begin of <f> occurs <n>, <component declaration>, ................................., End of <f>. ``` 范例- ``` Data : Begin of itab occurs 10, column1 type I, column2(4) type C, column3 like mara-ernam, End of itab. ``` 内部表 *Itab* 已创建 ## 填充内部表 Now that we have successfully created some internal tables, let us see how do we populate them with some records. There are various methods available to populate tables**1.Append Data line by line**The first method available is the use of the APPEND statement. Using the APPEND statement we can either add one line from another work area to the internal table or we can add one initial line to the internal table.. Syntax - ``` APPEND [<wa> TO / INITIAL LINE TO] <itable>. ``` Here work area *<wa>* or the Initial Line is appended to the internal table *<itable>.* The system variable *SY-TABIX* contains the index of the appended line. Example: ``` Data: Begin of itab occurs 10, col1 type C, col2 type I, end of itab. Append initial line to itab. ``` Results : ' ' '0' Initial lines adds a line initialized with the correct value for its type to the table. Here ,  col1 is an character and col2 is a integer. Then APPEND initial line, adds a line initialized with respect to the data type of the columns, i.e. space for col1 and 0 for col2. **2.Using COLLECT statement**  COLLECT is another form of statement used for populating the internal tables. Generally COLLECT is used while inserting lines into an internal table with unique standard key. Syntax- ``` COLLECT [<wa> INTO] <itable>. ``` Incase of tables with Header line, INTO option is omitted. Suppose there is already an entry having a key same as the one you are trying to append, then a new line is not added to the table, but the numeric fields of both the entries are added and only one entry corresponding to the key is present. Value of SY-TABIX is changed to the row of the original entry. Else COLLECT acts similar to APPEND and SY-TABIX contains the index of the processed line. **3**.**Using INSERT statement** INSERT statement adds a line/work area to the internal table. You can specify the position at which the new line is to be added by using the *INDEX* clause with the INSERT statement. Syntax ``` INSERT [<wa> INTO / INITIAL LINE INTO] <itable> [index <idx>]. ``` Here,  the work area *<wa>* or INITIAL LINE is inserted into internal table <*itable>* at index *<idx>*. ## 复制内部表 The contents of one internal table can be copied to another by using the APPEND LINES or INSERT LINES statement. A more simpler way is to usetany of the following syntax's. ``` MOVE <itab1> To <itab2>. OR <itab1> = <itab2>. ``` These copy the contents of ITAB1 to ITAB2\. Incase of internal tables with header line we have to use [] inorder to distinguish from work area. So, to copy contents of internal tables with header line the syntax becomes, ``` itab1[] = itab2[]. ``` ## 读取内部表 We are now  familiar with the creation of internal tables and populating them with data. We will now see how do we actually use the data or retrieve the data from the internal tables. **1\. Using Loop -Endloop** One of the ways of accessing or reading the internal table is by using LOOP-ENDLOOP. Syntax ``` LOOP AT <itable> [INTO <wa>] ................................... ENDLOOP. ``` Here when you say LOOP AT ITABLE, then the internal table ITABLE is read line by line. You can access the values of the columns for that line during any part of the LOOP-ENDLOOP structure. The value of the *SY-SUBRC* is set to **0**, even if only one record is read. **2\. Using READ** The other method of reading the internal table is by using the READ statement. Syntax- ``` READ TABLE <itable> [INTO <wa>] INDEX <idx>. ``` This statement reads the current line or line as  specified by index *<idx>*. The value of *SY-TABIX* is the index of the line read. If an entry with the specified index is found, then *SY-SUBRC* is set to 0\. If the specified index is less than 0, then run-time error occurs. If the specified index exceeds table size then SY-SUBRC is set to 4. ## 删除内部表 There are many ways for deleting lines from an internal table. **1.Deleting lines in a loop.** This is the simplest way for deleting lines. Sytax ``` DELETE <ITABLE>. ``` This statement works only within a loop. It deletes the current line. You can delete the lines in a loop conditionally by adding the WHERE clause. **2.Deleting lines using the index.** This is used to delete a line from internal table at any know index. Syntax ``` DELETE <ITABLE> INDEX <IDX>. ``` The line with the index <IDX> is deleted. The index of the following line is decremented by 1.