| Subject | Notes |
|
Reading a row from a database table into a structure that has a compatible type
|
Note that, since database tables are flat, the
target structure must also be flat. In the example below, the [`SINGLE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapselect_single.htm)
addition reads only a single row into the structure. It returns the first entry that matches the `WHERE` condition.
``` abap
"Creating a structure with a compatible type
DATA ls_fli1 TYPE zdemo_abap_fli.
SELECT SINGLE FROM zdemo_abap_fli
FIELDS *
WHERE carrid = 'LH'
INTO @ls_fli1.
"Target structure declared inline
SELECT SINGLE FROM zdemo_abap_fli
FIELDS *
WHERE carrid = 'LH'
INTO @DATA(ls_fli2).
```
|
|
Reading a row from a database table into a structure that has an incompatible type
|
Components in the structure with identical names are filled.
``` abap
SELECT SINGLE FROM zdemo_abap_fli
FIELDS *
WHERE carrid = 'AA'
INTO CORRESPONDING FIELDS OF @ls_fli_diff.
```
|
|
Reading a line from an internal table into a structure using an ABAP SQL `SELECT` statement
|
Note the specified alias name and that ABAP variables like internal tables must be escaped with `@`. The addition `INTO CORRESPONDING FIELDS OF` also applies here.
``` abap
SELECT SINGLE FROM @itab AS itab_alias
FIELDS *
WHERE ...
INTO @DATA(ls_struc).
"INTO CORRESPONDING FIELDS OF @some_existing_struc.
```
|
|
Sequentially reading a row from a database table into a structure
|
A `SELECT` loop can be specified with the syntax `SELECT ... ENDSELECT.`.
In the following example, the row found and returned in a structure declared inline can be processed further.
``` abap
SELECT FROM zdemo_abap_fli
FIELDS *
WHERE carrid = 'AZ'
INTO @DATA(ls_sel_loop).
IF sy-subrc = 0.
...
ENDIF.
ENDSELECT.
```
|
|
Inserting a single row into a database table from a structure using ABAP SQL statements with
[`INSERT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapinsert_dbtab.htm)
|
The following statements can be considered as alternatives. The third statement shows that instead of inserting a row from an existing structure, you can create and fill a structure directly.
Note that you should avoid inserting a row with a particular key into the database table if a row with the same key already exists. Note that with this and the followig syntax, various options/expressions are possible.
``` abap
INSERT INTO dbtab VALUES @struc.
INSERT dbtab FROM @struc.
INSERT dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).
```
|
|
Updating a single row in a database table from a structure using ABAP SQL statements with [`UPDATE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapupdate.htm)
|
Note that this syntax changes the entire row and all of its components.
``` abap
UPDATE dbtab FROM @struc.
UPDATE dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).
```
If you want to update a database table row from a structure by specifying components to be changed without overwriting other components, you can use the following method. First, read the desired row from the database table into a structure. Then, use the `VALUE` operator with the `BASE` addition and specify the components to be changed.
``` abap
SELECT SINGLE *
FROM dbtab
WHERE ...
INTO @DATA(wa).
UPDATE dbtab FROM @( VALUE #( BASE wa comp2 = ... comp4 = ... ) ).
```
|
|
Updating or creating a single row in a database table from a structure using ABAP SQL statements with
[`MODIFY`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapmodify_dbtab.htm)
|
If a row with the same key as specified in the structure already exists in the database table, the row is updated. If no row with the keys specified in the structure exists, a new row is created in the database table.
``` abap
MODIFY dbtab FROM @struc.
MODIFY dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).
```
|
|
Deleting a single row in a database table from a structure using ABAP SQL statements with
[`DELETE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapdelete_dbtab.htm)
|
If a row with the same key as specified in the structure already exists in the database table, the row is updated. If no row with the keys specified in the structure exists, a new row is created in the database table.
``` abap
DELETE dbtab FROM @struc.
DELETE dbtab FROM @( VALUE #( comp1 = ... ) ).
```
|
### Structures in Statements for Processing Internal Tables
The following code snippets cover a selection. Find more information and code snippets in the [Internal Tables](01_Internal_Tables.md) cheat sheet.
| Subject | Notes |
|
Reading a line from an internal table into a structure using a `READ TABLE` statement
|
The code snippet below shows the reading of a line into a [work area](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenwork_area_glosry.htm "Glossary Entry"), a [field symbol](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfield_symbol_glosry.htm "Glossary Entry"), and a [data reference variable](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendata_reference_variable_glosry.htm "Glossary Entry"), all of which
represent structured data objects that are declared inline. In the following example, a line is read based on the line number by
specifying `INDEX`. For more details, see the section *Determining the target area* in the cheat sheet [Internal Tables](01_Internal_Tables.md#).
``` abap
READ TABLE itab INTO DATA(wa) INDEX 1.
READ TABLE itab ASSIGNING FIELD-SYMBOL() INDEX 2.
READ TABLE itab REFERENCE INTO DATA(dref) INDEX 3.
```
|
|
Reading a line from an internal table into a structure using a [table expression](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentable_expression_glosry.htm "Glossary Entry")
|
The code snippet shows how to read a line into a structure declared inline. The index is given in square brackets. You can also specify table keys and free keys.
``` abap
DATA(ls_table_exp) = itab[ 3 ].
```
|
|
Sequentially reading a line from an internal table into a structure using a [`LOOP AT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abaploop_at_itab_variants.htm) statement
|
There are many ways to specify the condition on which the loop is based. The following example covers the option of reading all lines sequentially into a field symbol declared inline. When using a field symbol, you can, for example, directly modify components.
``` abap
LOOP AT itab ASSIGNING FIELD-SYMBOL().
-comp1 = ...
...
ENDLOOP.
```
|
|
Adding lines to and updating single lines in an internal table from a structure using `INSERT`,
`APPEND`, and `MODIFY` statements
|
- Note that all statements, including `INSERT` and `MODIFY`, are ABAP statements in this context, not ABAP SQL statements.
- Both `INSERT` and `APPEND` add one or more lines to an internal table. While `APPEND` adds at the bottom of the
internal table, `INSERT` can be used to add lines at a specific position in the table. If you do not specify the position, the lines are also added at the bottom of the table. However, unlike `APPEND`, `INSERT` does not set `sy-tabix`.
- `MODIFY` changes the content of an internal table entry.
- Statements using the `VALUE` operator to directly create and populate the structures are also possible. For more information and code
snippets, see the [Internal Tables](01_Internal_Tables.md#) cheat sheet.
``` abap
INSERT struc INTO TABLE itab.
APPEND struc TO itab.
MODIFY TABLE itab FROM struc.
```
|