Entering content frameLogical Databases and Contexts Locate the document in its SAP Library structure

This section introduces logical databases and contexts - two methods that make it easier to read data from database tables. Both of them encapsulate Open SQL statements in separate ABAP programs.

Logical Databases

Logical databases are special ABAP programs that read data from database tables. They are used by executable (type 1) programs. At runtime, you can regard the logical database and the executable program (reports) as a single ABAP program, whose processing blocks are called by the runtime environment in a particular, pre-defined sequence.

You edit logical databases using a tool within the ABAP Workbench, and link them to executable programs (reports) when you enter the program attributes. You can use a logical database with any number of executable programs (reports). From Release 4.5A, it is also possible to call logical databases on their own.

Structure of a Logical Database

The following diagram shows the structure of a logical database, which can be divided into three sections:

This graphic is explained in the accompanying text

Structure

The structure of a logical database determines the database tables which it can access. It adopts the hierarchy of the database tables defined by their foreign key relationships. This also controls the sequence in which the tables are accessed.

Selection Part

The selection part of the logical database defines input fields for selecting data. The runtime environment displays these on the selection screen when you run an executable program linked to the logical database. The corresponding fields are also available in the ABAP program, allowing you, for example, to change their values to insert default values on the selection screen.

Database Program

The database program of a logical database is a container for special subroutines, in which the data is read from the database tables. These subroutines are called by the reporting processor in the runtime environment in a predefined sequence.

Running Type 1 Programs with a Logical Database

The following diagram shows the principal processing blocks that are called when you run an executable program linked to a logical database:

This graphic is explained in the accompanying text

The runtime environment calls depend both on the structure of the logical database and on the definition of the executable program. The structure of the logical database determines the sequence in which the processing blocks of the logical database are called. These in turn call GET event blocks in the executable program. These GET event blocks determine the read depth in the structure of the logical database. TABLES or NODES statements in the declaration part of the executable program determine which of the input fields defined in the logical database are included in the selection screen. They also define interface work areas for passing data between the logical database and the executable program.

The actual access to the R/3 System database is made using OPEN SQL statements in the PUT_<TABLE> subroutines. The data that is read is passed to the executable program using the interface work areas (defined using the TABLES statement). Once the data has been read in the logical database program, the executable program (report) can process the data in the GET event blocks. This technique separates data reading and data processing.

Uses of Logical Databases

The main use of logical databases is to make the code that accesses data in database tables reusable. SAP supplies logical databases for all applications. These have been configured for optimal performance, and contain further functions such as authorization checks and search helps. It is appropriate to use logical databases whenever the database tables you want to read correspond largely to the structure of the logical database and where the flow of the system program (select - read - process - display) meets the requirements of the application.

Contexts

In application programming, you often use a relatively small set of basic data to derive further data. This basic data might, for example, be the data that the user enters on the screen. The relational links in the database are often used to read further data on the basis of this basic data, or further values are calculated from it using ABAP statements.

It is often the case that certain relationships between data are always used in the same form to get further data, either within a single program or in a whole range of programs. This means that a particular set of database accesses or calculations is repeatedly executed, despite the fact that the result already exists in the system. This causes unnecessary system load, which can be alleviated by using contexts.

You define contexts in the Context Builder, which is part of the ABAP Workbench. They consist of key input fields, the relationships between the fields, and the other fields and values that you can derive from them. Contexts can link these derived fields by foreign key relationships between tables, by function modules, or by other contexts.

This graphic is explained in the accompanying text

In application programs, you work with instances of a context. You can use more than one instance of the same context. The application program supplies input values for the key fields in the context using the SUPPLY statement, and can query the derived fields from the instance using the DEMAND statement.

Each context has a cross-transaction buffer on the application server. When you query an instance for values, the context program searches first of all for a data record containing the corresponding key fields in the appropriate buffer. If one exists, the data is copied to the instance. If one does not exist, the context program derives the data from the key field values supplied and writes the resulting data record to the buffer.

 

 

 

Leaving content frame