> For the complete documentation index, see [llms.txt](https://dingyj.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dingyj.gitbook.io/blog/sap/abap/working-with-internal-tables.md).

# Working with Internal Tables

## 追加数据

### APPEND&#x20;

* APPEND statement **always adds the record at the end of the table** and is used with standard tables.

> You need to use caution when using an APPEND statement with sorted tables; the system may **produce a runtime error if the new row does not match the sort sequence** of the table or if the table is defined with a unique key and the new row creates a duplicate entry

* Recommend always using the INSERT statement with sorted tables.
* Can't use APPEND statements with hashed tables, because they only support INSERT statements.

To add a record to an internal table, first fill the work area with the required data and then append it to the table using the APPEND statement.

```
TYPES : BEGIN OF ty_sflight.
        CARRID TYPE S_CARR_ID.
        CONNID TYPE S_C0NN_ID, 
        FLDATE TYPE S_DATE, 
END OF ty_sf1ight.

DATA : 
        1t_sflight TYPE STANDARD TABLE OF ty_sflight, 
        wa_sflight TYPE ty_sflight.

wa_sflight-carrid - *AA’. 
wa_sflight-connid = '0004’. 
wa_sflight-fldate = "20140101••APPEND wa_sflight TO it_sf1ight. "Adds the first row to the table wa_sflight-carrid = 'AB'. 
wa_sflight-connid = '0005'. 
wa_sflight-fldate = '20140102’.

APPEND wa_sflight TO it_sf1ight. "Adds second row to the table
```

### INSERT

* INSERT statement behaves like an APPEND statement for standard tables in that it adds a record at the end of the table if no explicit index is speci­ fied
* With sorted tables, the INSERT statement **automatically selects the posi­tion** based on the sort sequence of the table.

When an index is supplied with the INSERT statement, the record is **added explicitly in that row.** If the index supplied is greater than the line of the table, no record is inserted and the system field **sy-subrc is set to 4**.&#x20;

```
wa_sflight-carrid = 'AC'. 
wa_sflight-connid = '0006'. 
wa_sflight-fldate = '20140103'.
INSERT wa_sflight INTO TABLE it_sflight.

wa_sflight-carrid = 'AD'.
wa_sflight-connid = ’0007’. 
wa_sflight-fldate = ’20140104'.
INSERT wa_sf1ight INTO it.sflight INDEX 1
```
