📜 ⬆️ ⬇️

ABAP: Sampling reference values ​​by their keys from database tables

I wrote not so long ago a method for sampling reference values ​​by their keys into an internal table by creating a dynamic program. Unfortunately, the source code can not be applied, as the authorities have forbidden to do it. So I confine myself to a general description of the theory.

For example, we have an internal table:

DATA: begin of it_TABLE occurs 0, BE type T001-BUKRS, BENAME type T001-BUTXT, end of it_TABLE . 

In the table, the BE field is filled in and we need to select the BUTXT value from the corresponding BE from T001 and fill in the BENAME field. How to do it "correctly" (ie, with minimal memory and as quickly as possible).

  1. Choose unique BE field values;
  2. Make FOR ALL ENTRIES of T001;
  3. Let's go through the internal table and for each BE we find the corresponding value in the data selected from T001. (Of course, we use the HASH table to speed up the search)
And this sequence is always the same.
Here is the code for this process.
 FIELD-SYMBOLS: <wa_Table> like line of it_Table . *--      BE TYPES: BEGIN OF s_k1, BUKRS type T001-BUKRS, END OF s_k1 . DATA: it_k1 TYPE SORTED TABLE OF s_k1 WITH UNIQUE KEY BUKRS , wa_k1 like line of it_k1. LOOP AT it_Table ASSIGNING <wa_Table>. wa_k1-BUKRS = <wa_Table>-BE. INSERT wa_k1 INTO TABLE it_k1. ENDLOOP. *--     T001    TYPES: BEGIN OF s_v1, BUKRS TYPE T001-BUKRS, BUTXT TYPE T001-BUTXT, END OF s_v1 . DATA: it_v1 TYPE SORTED TABLE OF s_v1 WITH NON-UNIQUE KEY BUKRS , wa_v1 like line of it_v1. IF it_k1[] IS INITIAL. REFRESH it_v1. ELSE. SELECT BUKRS BUTXT FROM T001 INTO CORRESPONDING FIELDS OF TABLE it_v1 FOR ALL ENTRIES in it_k1 WHERE BUKRS = it_k1-BUKRS . ENDIF. *--     LOOP AT it_Table ASSIGNING <wa_Table>. READ TABLE it_v1 INTO wa_v1 with table key BUKRS = <wa_Table>-BE . IF sy-subrc = 0. <wa_Table>-BENAME = wa_v1-BUTXT. ELSE. CLEAR <wa_Table>-BENAME. ENDIF. ENDLOOP. 
And if we always perform the same actions, it means that their description should be reduced to a minimum. In particular, in this case, we need to specify the table from which to select, the key fields and the fields from which to write data to.
')
Those. we have the following small line in which the whole essence of the above code is written:
  'T001{BUKRS=BE}{BUTXT>BENAME}' 

We pass to the method the string specified above + our internal table. And after calling the method, the BENAME field will be filled with the corresponding values ​​from T001-BUTXT. We have reduced our code, besides another programmer it is enough to have a method reference at hand and he will quickly understand what this line is doing. In this case, the whole algorithm fits into a small line.
Here is the code generated by my method.
  TYPES: BEGIN OF s_k1, BUKRS type T001-BUKRS, END OF s_k1 . DATA: it_k1 TYPE SORTED TABLE OF s_k1 WITH UNIQUE KEY BUKRS , wa_k1 like line of it_k1. FIELD-SYMBOLS: <fs_1_BUKRS> type ANY, <fs_1_BUTXT> type ANY . "--      BE LOOP AT <it_Table> ASSIGNING <wa_Table>. "-- ASSIGN COMPONENT 'BE' OF STRUCTURE <wa_Table> TO <fs_1_BUKRS>. wa_k1-BUKRS = <fs_1_BUKRS>. "--     INSERT wa_k1 INTO TABLE it_k1. ENDLOOP. TYPES: BEGIN OF s_v1, BUKRS TYPE T001-BUKRS, BUTXT TYPE T001-BUTXT, END OF s_v1 . DATA: it_v1 TYPE SORTED TABLE OF s_v1 WITH NON-UNIQUE KEY BUKRS , wa_v1 like line of it_v1. *--    T001 IF it_k1[] IS INITIAL. REFRESH it_v1. ELSE. SELECT BUKRS BUTXT FROM T001 INTO CORRESPONDING FIELDS OF TABLE it_v1 FOR ALL ENTRIES in it_k1 WHERE BUKRS = it_k1-BUKRS . ENDIF. FREE it_k1. *--     LOOP AT <it_Table> ASSIGNING <wa_Table>. ASSIGN COMPONENT 'BE' OF STRUCTURE <wa_Table> TO <fs_1_BUKRS>. ASSIGN COMPONENT 'BENAME' OF STRUCTURE <wa_Table> TO <fs_1_BUTXT>. "--   READ TABLE it_v1 INTO wa_v1 with table key BUKRS = <fs_1_BUKRS> . IF sy-subrc = 0. <fs_1_BUTXT> = wa_v1-BUTXT. ELSE. CLEAR: <fs_1_BUTXT> . ENDIF. ENDLOOP. FREE it_v1. 
For the full work of the method, we will add the ability to specify constants and system variables (for example, sy-langu is often required for selecting texts). Also add the ability to specify a list of commands - then we will be able to consistently select data.

How it works? - using dynamic programs. Those. the method analyzes the parameters passed through the string, generates a dynamic program and starts it. The program is added through the “INSERT REPORT l_repid FROM CODE.” In order not to be limited by the number of programs created.

PS I am especially pleased with this method when I have to choose a lot of values, and from time to time in the process of work, the director receives an indication to add a new field for selection.

update : the process code was written in the correct form - it turned out 53 lines. Parameter line + method call = 5 lines. Those. the code has been reduced by 10 times and now it is placed within one page. Which, in my opinion, greatly improves readability.

Source: https://habr.com/ru/post/244615/


All Articles