Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  Classic Objects in ABAP Dictionary →  Views →  Classic Views 

Database Views

A database view is a general view for one or more basis tables. If there are multiple basis tables, they are joined using an inner join. A matching SQL view is created as a database object on the database when the view is activated. The structure type defined using the view fields of the database view can be referenced in ABAP programs using TYPE. A database view can be accessed using ABAP SQL, Native SQL, or AMDP in CDS views, and also from outside AS ABAP using the programming interface of the database. The latter method is, however, not recommended.

Other versions: 7.31 | 7.40 | 7.54


Notes

  • A database view can be activated in ABAP Dictionary without its basis tables being defined on the database. The view is also not created on the database in this case.

  • As in a database table, the client dependency of a database view is determined by an initial column with the built-in dictionary type CLNT.

  • Append views are available for enhancing the database views in ABAP Dictionary delivered by SAP without making any modifications.

Basis Tables

The basis tables of database views can be transparent tables and global temporary tables.


Note

The basis tables of CDS database views that are generated for CDS views can also be other database views.

View Fields

An include mechanism can be used to add individual fields or all fields from the basis tables as view fields of a database view. When individual fields are used, a name other than the name in the basis tables.can be defined for a view field. This name can have a maximum of 30 characters, must meet the naming conventions for component names of structures, and cannot be a reserved name. The reserved names that cannot be used are specified in the database table TRESE. Fields with the type CHAR can have a maximum of 1333 characters. Fields with the type LRAW or LCHR must be at the end of the view. Only one such field is allowed per view. Furthermore, a field with the type INT2 or INT4 (for the maximum length of the field) must be located directly in front of this field.

To use all fields, the character is used instead of the field name. If the - character is used for individual fields of a basis table included using , all fields up to the fields specified with - become view fields. If the structure of a database table whose fields were added in this way is modified, the structure of the view is also modified.


Notes

  • If a foreign key field is added as a view field, it keeps this attribute and all associated foreign key fields become the foreign key of the view.

  • A database view in the software component SAP_BASIS can contain a maximum of 749 view fields and the total of the field lengths (the number of bytes in non-character-like fields plus the number of characters in flat character-like fields) must not exceed 4096. A database view in any other software component can contain a maximum of 1500 view fields and the total of the field lengths is not checked in ABAP Dictionary.

Join Conditions

The join conditions for the inner join can be formulated using equality relationships between any two table fields in the basis tables. The inner join joins the rows of the basis tables in question as a results set containing all combinations of rows whose columns together meet the join conditions. If there are no rows that meet the join conditions in the basis tables in question, no row is created in the results set. If there is a suitable foreign key dependency (including generic and constant foreign keys) between two basis tables, this dependency can be used as a default for defining join conditions in ABAP Dictionary.

Maintenance Status

The Access setting in the maintenance status of a database view can have the following values:

  • Read only
The database view can only be used to read data with ABAP SQL.
  • Read, change, delete, and insert
If the database view contains only a single basis table, data can be changed using ABAP SQL.

A database view with multiple basis tables can only be read with ABAP SQL. If a database view contains only a single table, data can be inserted in this table using the view with the ABAP SQL statements INSERT or MODIFY. The following options are available for the content of the table fields not in the view:

  • If the table field is defined with NOT NULL on the database, the field is filled with the corresponding initial value.
  • If the table field is defined with NOT NULL on the database, and there is no initial value, nothing can be inserted and a database error occurs with a corresponding exception.
  • If the table field is not defined with NOT NULL on the database, the field is filled with the null value.


Notes

  • A database view should only be used to insert data in the table if the flag for initial values is set for all table fields that are not in the view.

  • It is not usually a problem to modify existing data records using a database view in cases where the database view contains all key fields of the table.

  • Like a database table, the maintenance status of a database view has a Display and Maintain setting alongside the Access setting. The setting must match the Access setting. In database views containing multiple basis tables, only the setting Display/maintenance not allowed is possible.

Table Buffering

Just like in database tables, it is possible to define for a database view whether the data in the table buffer is buffered. The same buffering types can be defined and table buffering in ABAP SQL is handled in the same way for views and for database tables with the only difference being that, when data is modified in one of the basis tables, the entire buffer is invalidated when the buffers are synchronized, regardless of the buffering type In client-specific views, however, this only affects the content of the current client.

The following prerequisites must be met before a database view can be buffered:

  • A basis table can occur in no more than nine buffered database views.
  • If all fields of the database view are key fields, these fields must also be key fields of the basis tables.


Note

No replacement objects can be created for an existing buffered view whose data can be written to using ABAP SQL.


Example

The database view DEMO_SCARR_SPFLI contains fields of the tables SCARR and SPFLI. Other names are assigned to these fields in the view.

The associated database object could be as follows:

CREATE VIEW DEMO_SCARR_SPFLI
   (CLIENT,
   ID,
   CARRIER,
   FLIGHT,
   DEPARTURE,
   DESTINATION )
AS SELECT
   T1."MANDT",
   T2."CARRID",
   T1."CARRNAME",
   T2."CONNID",
   T2."CITYFROM",
   T2."CITYTO"
FROM
    "SCARR" T1,
    "SPFLI" T2
WHERE
    T1."MANDT"  = T2."MANDT" AND
    T1."CARRID" = T2."CARRID"

The view can be accessed as follows using ABAP SQL:

SELECT * 
       FROM demo_scarr_spfli 
       ORDER BY id, carrier, flight 
       INTO TABLE @DATA(result). 

cl_demo_output=>display( result ).