Entering content frameCalling a Logical Database Using a Function Module Locate the document in its SAP Library 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 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:

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

The parameters are filled by the function module LDB_PROCESS. They have the following meaning:

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.

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

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

Example

Example

TABLES SPFLI.
SELECT-OPTIONS S_CARR FOR 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.

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                     = ' '
          EXPRESSIONS                 = TEXPR
          FIELD_SELECTION             = FSEL
     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:

This graphic is explained in the accompanying text

A program-specific selection screen is defined at the beginning of the program. This requires the TABLES statement. 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 program then calls the function module LDB_PROCESS 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:

This graphic is explained in the accompanying text

 

 

 

Leaving content frame