Logical databases are special ABAP programs that retrieve data and make it available to application programs. The most common use of logical databases is still to read data from database tables by linking them to executable ABAP programs.
However, from Release 4.5A, it has also been possible to call logical databases using the function module LDB_PROCESS. This allows you to call several logical databases from any ABAP program, nested in any way. It is also possible to call a logical database more than once in a program, if it has been programmed to allow this. This is particularly useful for programs with type 1.
Logical databases contain Open SQL statements that read data from the database. You do not therefore need to use SQL in your own programs. The logical database reads the program, stores them in the program if necessary, and then passes them line by line to the application program or the function module LDB_PROCESS using an interface work area.
Logical Databases – Views of Data
A logical database provides a particular view of database tables in the R/3 System. It is always worth using logical databases if the structure of the data that you want to read corresponds to a view available through a logical database.
The data structure in a logical database is hierarchical. Many tables in the R/3 System are linked to each other using foreign key relationships. Some of these dependencies form tree-like hierarchical structures. Logical databases read data from database tables that are part of these structures.
The diagram illustrates how the R/3 System might represent the structure of a company. A logical database can read the lines of these tables one after the other into an executable program in a sequence which is normally defined by the hierarchical structure. The term logical database is sometimes used to mean not only the program itself, but also the data that it can procure.
Tasks of Logical Databases
As well as allowing you to read data from the database, logical databases also allow you to program other tasks centrally, making your application programs less complicated. They can be used for the following tasks:
- Reading the same data for several programs.
The individual programs do not then need to know the exact structure of the relevant database tables (and especially not their foreign key relationships). Instead, they can rely on the logical database to read the database entries in the right order during the GET event.
- Defining the same user interface for several programs.
Logical databases have a built-in selection screen. Therefore, all of the programs that use the logical database have the same user interface.
- Central authorization checks
Authorization checks for central and sensitive data can be programmed centrally in the database to prevent them from being bypassed by simple application programs.
- Improving performance
If you want to improve response times, logical databases permit you to take a number of measures to achieve this (for example, using joins instead of nested SELECT statements). These become immediately effective in all of the application programs concerned and save you from having to modify their source code.
There are two ways of using a logical database: Either by linking it with an executable program, or by using the function module LDB_PROCESS in any ABAP program.
When you link a logical database to an executable program, the user can enter values on the selection screen, and the data read by the logical database is passed back to the program using the interface work areas. If you call the logical database using a function module, the selection screen is not displayed. The calling program does not have to provide interface work areas. Instead, it uses special subroutines called callback routines, which are called by the function module and filled with the required data.
From Release 4.5A it is possible to call logical databases independently from any ABAP program. Previously it was only possible to link a logical database to an executable program, in which the processing blocks of the logical database and the program were controlled by the ABAP runtime environment.
To call a logical database from another program, use the function module LDB_PROCESS. This allows you to use the logical database as a routine for reading data. You can call more than one logical database from the same program. You may also call the same logical database more than once from the same program. In the past, it was only possible to use a logical database more than once or use more than one logical database by calling a further executable program using SUBMIT. These programs had to be linked to the corresponding logical database, and the data had to be passed to the calling program using ABAP memory or a similar technique.
When you call a logical database using the function module LDB_PROCESS, its selection screen is not displayed. Instead, you fill the selections using the interface parameters of the function module. The logical database does not trigger any GET events in the calling program, but passes the data back to the caller in callback routines. Calling a logical database using LDB_PROCESS thus decouples the actual data retrieval from the preceding selection screen processing and the subsequent data processing.
There is no need to adapt a logical database for use with LDB_PROCESS, except in the following cases: If you do not adapt a logical database, it is not possible to use the function module to call the same logical database more than once. The PAI subroutine is not called when you use LDB_PROCESS. This means that none of the checks for selections programmed in it are performed. You can work around these restrictions by including the subroutines LDB_PROCESS_INIT and LDB_PROCESS_CHECK_SELECTIONS in the database program.
The subroutines in the logical database are called in the following sequence when you call the function module LDB_PROCESS:
- PUT <node>.
None of the subroutines used to process the selection screen when you link the logical database to an executable program are called, neither does the runtime environment trigger any reporting events in the calling program. Instead, the PUT statements in the logical database trigger actions in the function module that call callback routines in the calling program. In other words, the function module catches the events that are otherwise processed by the runtime environment.
Parameters of LDB_PROCESS
The function module has the following import parameters:
Name of the logical database you want to call.
Name of a variant to fill the selection screen of the logical database. The variant must already be assigned to the database program of the logical database. The data is passed in the same way as when you use the WITH SELECTION-TABLE addition in a SUBMIT statement.
In this parameter, you can pass extra selections for the nodes of the logical database for which dynamic selections are allowed. The data type of the parameter RSDS_TEXPR is defined in the type group RSDS. The data is passed in the same way as when you use the WITH FREE SELECTION addition in a SUBMIT statement.
You can use this parameter to pass a list of the required fields for the nodes of the logical database for which dynamic selections are allowed. The data type of the parameter is the deep internal table RSFS_FIELDS, defined in the type group RSFS. The component TABLENAME contains the name of the node and the deep component FIELDS contains the names of the fields that you want to read.
The function module has the following tables parameters:
You use this parameter to assign callback routines to the names of nodes and events. The parameter determines the nodes of the logical database for which data is read, and when the data is passed back to the program and in which callback routine.
You can use this parameter to pass input values for the fields of the selection screen of the logical database. The data type of the parameter corresponds to the structure RSPARAMS in the ABAP Dictionary. The data is passed in the same way as when you use the WITH SELECTION-TABLE addition in a SUBMIT statement.
If you pass selections using more than one of the interface parameters, values passed in SELECTIONS and EXPRESSIONS overwrite values for the same field in VARIANT.
Read Depth and Callback Routines
When you link a logical database with an executable program, the GET statements determine the depth to which the logical database is read. When you call the function module LDB_PROCESS, you determine the depth by specifying a node name in the CALLBACK parameter. For each node for which you request data, a callback routine can be executed at two points. These correspond to the GET and GET LATE events in executable programs. In the table parameter CALLBACK, you specify the name of the callback routine and the required execution point for each node. A callback routine is a subroutine in the calling program or another program that is to be executed at the required point.
For the GET event, the callback routine is executed directly after the data has been read for the node, and before the subordinate nodes are processed. For the GET_LATE event, the callback routine is processed after the subordinate nodes have been processed.
The line type of the table parameter CALLBACK is the flat structure LDBCB from the ABAP Dictionary. It has the following components:
Name of the node of the logical database to be read.
A flag (contents X or SPACE), to call the corresponding callback routine at the GET event.
A flag (contents X or SPACE), to call the corresponding callback routine at the GET LATE event.
Name of the ABAP program in which the callback routine is defined.
Name of the callback routine.
If you pass an internal table to the CALLBACK parameter, you must fill at least one of the GET or GET_LATE columns with X for each node (you may also fill both with X).
A callback routine is a subroutine that must be defined with the following parameter interface:
FORM <subr> USING <node> LIKE LDBCB-LDBNODE
<wa> [TYPE <t>]
The parameters are filled by the function module LDB_PROCESS. They have the following meaning:
- <node> contains the name of the node.
- <wa> is the work area of the data read for the node. The program that calls the function module LDB_PROCESS and the program containing the callback routine do not have to declare interface work areas using NODES or TABLES. If the callback routine is only used for one node, you can use a TYPE reference to refer to the data type of the node in the ABAP Dictionary. Only then can you address the individual components of structured nodes directly in the subroutine. If you use the callback routine for more than one node, you cannot use a TYPE reference. In this case, you would have to address the components of structured nodes by
assigning them one by one to a field symbol.
- <evt> contains G or L, for GET or GET LATE respectively. This means that the subroutine can direct the program flow using the contents of <evt>.
- <check> allows the callback routine to influence how the program is processed further (but only if <evt> contains the value G). The value X is assigned to the parameter when the subroutine is called. If it has the value SPACE when the subroutine ends, this flags that the subordinate nodes of the logical database should not be processed in the function module LDB_PROCESS. This is the same as
leaving a GET event block using CHECK in an executable program. If this prevents unnecessary data from being read, it will improve the performance of your program.
Exceptions of LDB_PROCESS
A logical database may not be called if it is still processing a previous call. If this occurs, the exception LDB_ALREADY_RUNNING is triggered.
A logical database may only be called repeatedly if its database program contains the subroutine LDB_PROCESS_INIT, otherwise, this exception is triggered.
For details of further exceptions, refer to the function module documentation in the Function Builder.
DATA wa_spfli TYPE spfli.
SELECT-OPTIONS s_carr FOR wa_spfli-carrid.
*TYPE-POOLS: RSDS, RSFS.
DATA: callback TYPE TABLE OF ldbcb,
callback_wa LIKE LINE OF callback.
DATA: seltab TYPE TABLE OF rsparams,
seltab_wa LIKE LINE OF seltab.
*DATA: TEXPR TYPE RSDS_TEXPR,
* FSEL TYPE RSFS_FIELDS.
callback_wa-ldbnode = ‘SPFLI’.
callback_wa-get = ‘X’.
callback_wa-get_late = ‘X’.
callback_wa-cb_prog = sy-repid.
callback_wa-cb_form = ‘CALLBACK_SPFLI’.
APPEND callback_wa TO callback.
callback_wa-ldbnode = ‘SFLIGHT’.
callback_wa-get = ‘X’.
callback_wa-cb_prog = sy-repid.
callback_wa-cb_form = ‘CALLBACK_SFLIGHT’.
APPEND callback_wa TO callback.
seltab_wa-kind = ‘S’.
seltab_wa-selname = ‘CARRID’.
LOOP AT s_carr.
MOVE-CORRESPONDING s_carr TO seltab_wa.
APPEND seltab_wa TO seltab.
CALL FUNCTION ‘LDB_PROCESS’
ldbname = ‘F1S’
variant = ‘ ‘
* EXPRESSIONS = TEXPR
* FIELD_SELECTION = FSEL
callback = callback
selections = seltab
ldb_not_reentrant = 1
ldb_incorrect = 2
ldb_already_running = 3
ldb_error = 4
ldb_selections_error = 5
ldb_selections_not_accepted = 6
variant_not_existent = 7
variant_obsolete = 8
variant_error = 9
free_selections_error = 10
callback_no_event = 11
callback_node_duplicate = 12
OTHERS = 13.
IF sy-subrc <> 0.
WRITE: ‘Exception with SY-SUBRC’, sy-subrc.
FORM callback_spfli USING name TYPE ldbn-ldbnode
wa TYPE spfli
evt TYPE c
check TYPE c.
WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
FORM callback_sflight USING name TYPE ldbn-ldbnode
wa TYPE sflight
evt TYPE c
check TYPE c.
WRITE: / wa-fldate, wa-seatsocc, wa-seatsmax.