Search code examples
databaseabapopensql

Getting empty result with SELECT ... FOR ALL ENTRIES


I have the following problem.

I need to write a script that reads a specific file, from the presentation layer, containing one numerical string per line, the contents of this file need to be entered into an internal table, and then the internal table needs to be inner joined, with table dfkkop to produce the equivalent lines that correspond to each numerical string in the internal table.

I recently read that a conventional inner join cannot be done between an internal table ("itab") and a database table, and instead I should use FOR ALL ENTRIES IN. The problem is that it doesn't seem to work. Here's the code:

TYPES:
  BEGIN OF type_dfkkop_vkont,
    vkont TYPE dfkkop-vkont,
  END OF type_dfkkop_vkont.

DATA: gt_dfkkop_file TYPE STANDARD TABLE OF type_dfkkop_vkont,
      wa_gt_dfkkop_file LIKE LINE OF gt_dfkkop_file,
      gt_dfkkop LIKE DFKKOP OCCURS 0 WITH HEADER LINE,
      wa_gt_dfkkop LIKE dfkkop,
DATA: gv_filename TYPE string,
      gv_filetype TYPE char10.

START-OF-SELECTION.
  PERFORM read_file.
  PERFORM process_data.


FORM read_file.
  gv_filename = 'C:\Users\p.doulgeridis\Desktop\data.txt'.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename            = gv_filename
      filetype            = 'ASC'
      has_field_separator = 'X'
    TABLES
     data_tab            = gt_dfkkop_file.

ENDFORM.

FORM process_data.

  SELECT *
    FROM dfkkop
    INTO CORRESPONDING FIELDS OF TABLE gt_dfkkop
    FOR ALL ENTRIES IN gt_dfkkop_file
    WHERE vkont = gt_dfkkop_file-vkont.

  IF gt_dfkkop_file IS NOT INITIAL.
    WRITE: / 'TABLE GT_DFKKOP_FILE IS NOT INITIAL'.
  ELSE.
    WRITE: / 'TABLE GT_DFKKOP_FILE IS INITIAL'.
  ENDIF.

  IF gt_dfkkop IS NOT INITIAL.
    WRITE: / 'TABLE GT_DFKKOP IS NOT INITIAL'.
  ELSE.
    WRITE: / 'TABLE GT_DFKKOP IS INITIAL'.
  ENDIF.

  LOOP AT gt_dfkkop_file INTO wa_gt_dfkkop_file.
    WRITE: / wa_gt_dfkkop_file-vkont.
  ENDLOOP.

  LOOP AT gt_dfkkop INTO wa_gt_dfkkop.
    WRITE: / wa_gt_dfkkop-vkont, wa_gt_dfkkop-opbel, wa_gt_dfkkop-OPUPW.
  ENDLOOP.

ENDFORM.

At the moment, I do not receive a syntax error, the program executes, but produces no output whatsoever. Could someone help me?

Edit 1: The output looks like this, it's like it doesn't find the values in the text file to query with, keep in mind the file I'm reading has around 11 lines worth of values, which already exist in table dfkkop so they should be producing a result, instead I get:

[blank11spaces]             4000000187   000

Exact output:

enter image description here


Solution

  • In the for all entries part you have to refer to the internal table containing the key entries. Forget ENDSELECT also because you can move all entries into the internal table at once.

    The proper syntax would be:

      SELECT *
        FROM dfkkop
        INTO CORRESPONDING FIELDS OF TABLE gt_dfkkop
        FOR ALL ENTRIES IN gt_dfkkop_file
        WHERE vkont = gt_dfkkop_file-vkont.
    

    More about the command here. Btw, the most important part of this command is the following, keep this in mind:

    Before using an internal table itab after FOR ALL ENTRIES, always check that the internal table is not initial. In an initial internal tables, all rows are read from the database regardless of any further conditions specified after WHERE. This is not usually the required behavior.