Show TOC

Calling a Logical Database Using a Function ModuleLocate this document in the navigation structure

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 and parameters 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.

Runtime Behavior

The subroutines in the logical database are called in the following sequence when you call the function module LDB_PROCESS:

  1. LDB_PROCESS_INIT
  2. INIT
  3. LDB_PROCESS_CHECK_SELECTIONS
  4. 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:

  • LDBNAME

    Name of the logical database you want to call.

  • VARIANT

    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.

  • EXPRESSIONS

    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 theWITH FREE SELECTIONaddition in a SUBMIT statement.

  • FIELD_SELECTION

    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:

  • CALLBACK

    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.

  • SELECTIONS

    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:

  • LDBNODE

    Name of the node of the logical database to be read.

  • GET

    A flag (contents X or SPACE), to call the corresponding callback routine at the GETevent.

  • GET_LATE

    A flag (contents X or SPACE), to call the corresponding callback routine at the GET LATEevent.

  • CB_PROG

    Name of the ABAP program in which the callback routine is defined.

  • CB_FORM

    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]                evt                check.

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 aTYPE 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 LATErespectively. 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
  • LDB_ALREADY_RUNNING

    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.

  • LDB_NOT_REENTRANT

    A logical database may only be called repeatedly if its database program contains the subroutine LDB_PROCESS_INIT , otherwise, this exception is triggered.

  • LDB_SELECTIONS_NOT_ACCEPTED

    Error handling in the subroutine LDB_PROCESS_CHECK_SELECTIONS of the database program can trigger this exception. The error message is provided in the usual system fields SY-MSG... .

For details of further exceptions, refer to the function module documentation in the Function Builder.

Example
Tip

REPORT demo_logical_database.

DATA wa_spfli TYPE spfli.SELECT-OPTIONS s_carr FOR wa_spfli-carrid.

DATA: callback TYPE TABLE OF ldbcb,      callback_wa LIKE LINE OF callback.

DATA: seltab TYPE TABLE OF rsparams,      seltab_wa LIKE LINE OF seltab.

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.

CLEAR callback_wa.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.ENDLOOP.

CALL FUNCTION 'LDB_PROCESS'     EXPORTING          ldbname                     = 'F1S'          variant                     = ' '     TABLES          callback                    = callback          selections                  = seltab     EXCEPTIONS          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.ENDIF.

FORM callback_spfli USING name  TYPE ldbn-ldbnode                          wa    TYPE spfli                          evt   TYPE c                          check TYPE c.  CASE evt.    WHEN 'G'.      WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.      ULINE.    WHEN 'L'.      ULINE.  ENDCASE.ENDFORM.

FORM callback_sflight USING name  TYPE ldbn-ldbnode                            wa    TYPE sflight                            evt   TYPE c                            check TYPE c.  WRITE: / wa-fldate, wa-seatsocc, wa-seatsmax.ENDFORM.

The program is written to read data using the logical database F1S. The structure of F1S is:

A program-specific selection screen is defined at the beginning of the program. The data object wa_spfli is required for this definition. Next, the required variables are defined for the interface.

The internal table CALLBACK is filled so that various callback routines are called in the program for the two nodes SPFLI and SFLIGHT. For SPFLI, the routine is to be called for GET and GET_LATE, for SFLIGHT, only at the GET event.

The internal table SELTAB is filled with values for the node SPFLI from the selection table S_CARR from the program-specific selection screen.

The function module LDB­_PROCESS is called with these parameters.

The subroutines CALLBACK_SPFLI and CALLBACK_SFLIGHT serve as callback routines. The interface parameter WA is fully typed, so you can address the individual components of the work areas. The events GET and GET LATE are handled differently in CALLBACK_SPFLI.

The beginning of the list output might look like this: