1. 程式人生 > >ABAP--通過LDB_PROCESS函式使用邏輯資料庫

ABAP--通過LDB_PROCESS函式使用邏輯資料庫

1、概覽
通過LDB_PROCESS函式可以允許任何程式訪問邏輯資料庫,允許一個程式訪問多個邏輯資料庫,當然也允許多次連續訪問訪問同個邏輯資料庫。當使用LDB_PROCESS函式來訪問邏輯資料庫時,選擇螢幕將不顯示,其選擇引數由FIELD_SELECTION引數傳入。

2、LDB_PROCESS引數說明
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 the WITH FREE SELECTION addition 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.


3、LDB_PROCESS的CALLBACK回撥引數的具體欄位的說明
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 GET event.

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

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

CB_FORM
Name of the callback routine.


4、回撥函式的編寫
回撥子程式的標準形式
FORM <subr> USING <node> LIKE LDBCB-LDBNODE
                  <wa>   [TYPE <t>]
                  <evt>
                  <check>.
......
ENDFORM.
其中引數說明作用:
<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.

5、樣例程式碼及說明
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.

*SPFLI節點對應的回撥處理函式
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.

*SFIGHT節點對應的回撥處理函式
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.

注意:通過'LDB_PROCESS'函式訪問邏輯資料庫時,請不要在程式屬性裡繫結邏輯資料庫,否則會出LDB_ALREADY_RUNNING錯誤。
資料來源sap library.