简体   繁体   English

ABAP - 内部连接 ​​itab 与 db 表

[英]ABAP - Inner Join itab with db table

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.我需要写一个脚本,从表现层读取一个特定的文件,每行包含一个数字字符串,这个文件的内容需要输入到一个内表中,然后需要将内表进行内连接,用table dfkkop 生成对应于内部表中每个数字字符串的等效行。

I recently read that a conventional inner join cannot be done between an itab and a db table, and instead I should use "FOR ALL ENTRIES IN".我最近读到不能在 itab 和 db 表之间进行传统的内部连接,而我应该使用“FOR ALL ENTRIES IN”。 The problem is that it doesnt 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.
DATA: wa_gt_dfkkop_file LIKE LINE OF gt_dfkkop_file.

DATA: gt_dfkkop LIKE DFKKOP OCCURS 0 WITH HEADER LINE. 
DATA: 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
*&---------------------------------------------------------------------*
FORM read_file.
*Move complete file path to file name
  gv_filename = 'C:\Users\p.doulgeridis\Desktop\data.txt'.

*Upload the data from a file in SAP presentation server to internal
*table
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename            = gv_filename
      filetype            = 'ASC'
      has_field_separator = 'X'
    TABLES
     data_tab            = gt_dfkkop_file.

ENDFORM.                    " read_file

*&---------------------------------------------------------------------*
*&      Form  process_data
*&---------------------------------------------------------------------*
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.

*Display the internal table data
*  LOOP AT gt_dfkkop_file INTO wa_gt_dfkkop_file.
*    WRITE:/ wa_gt_dfkkop_file-vkont.
*    CLEAR: wa_gt_dfkkop_file.
*  ENDLOOP.
*  

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: /"'1',
      wa_gt_dfkkop_file-vkont.
 ENDLOOP.

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

ENDFORM.                    " process_data

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:编辑 1:输出看起来像这样,就像在文本文件中找不到要查询的值一样,请记住我正在阅读的文件有大约 11 行值,这些值已经存在于表 dfkkop 中,所以他们应该产生结果,而不是我得到:

[blank11spaces]             4000000187   000

Exact output:精确输出:

在此处输入图片说明

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.忘记 ENDSELECT 也是因为您可以一次将所有条目移动到内部表中。

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.在 FOR ALL ENTRIES 之后使用内部表 itab 之前,请始终检查内部表是否不是初始表。 In an initial internal tables, all rows are read from the database regardless of any further conditions specified after WHERE.在初始内部表中,无论 WHERE 之后指定的任何其他条件如何,都会从数据库中读取所有行。 This is not usually the required behavior.这通常不是必需的行为。

The problem in your select is that you are using "FOR ALL ENTRIES IN gt_dfkkop" instead of " FOR ALL ENTRIES IN gt_dfkkop_file ", besides, you can use INTO instead of SELECT - ENDSELECT, that will lead to the query szako has done.您选择的问题是您使用的是“FOR ALL ENTRIES IN gt_dfkkop”而不是“ FOR ALL ENTRIES IN gt_dfkkop_file ”,此外,您可以使用INTO 而不是SELECT - ENDSELECT,这将导致szako 已完成的查询。

If you deal with large volumes of data and you expect to find many contract account (VKONT) duplicates in gt_dfkkop_file you can increase performance moving vkont to another temporal table and use it instead of gt_dfkkop_file.如果您处理大量数据并且希望在 gt_dfkkop_file 中找到许多合约帐户 (VKONT) 重复项,则可以通过将 vkont 移动到另一个时态表并使用它而不是 gt_dfkkop_file 来提高性能。 Check allways that the table you use is not empty.始终检查您使用的表是否为空。

szako seems to have answered this in the comment above: szako 似乎在上面的评论中回答了这个问题:

The DFKKOP-VKONT field has alpha conversion. DFKKOP-VKONT 字段具有 alpha 转换。 As I see the ids you have are 10 chars length.如我所见,您拥有的 id 长度为 10 个字符。 In the file do you have leading zeros?在文件中你有前导零吗? If not it will never find the corresponding records.如果不是,它将永远找不到相应的记录。 Use input alpha-conversion method for the input values.对输入值使用输入 alpha 转换方法。 – szako Jun 23 at 12:09 – szako 6 月 23 日 12:09

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM