如何将内部 table 分配到结构中,然后分配到 ABAP 中的字段

How to assign internal table into a structure and then to a field in ABAP

使用下面的代码我可以检索内部 table t_t005e 的内容,但是当放入字段 wa_upload-region 时,只检索数据的第一列,但是我想检索第三列数据。

TYPES: BEGIN OF ty_upload,
  " ...
  region TYPE regio,
  " ...
END OF ty_upload.

DATA: wa_upload TYPE ty_upload,
      t_t005e   TYPE STANDARD TABLE OF t005e.

READ TABLE t_t005e
           INTO wa_upload-region
           WITH KEY land1 = 'GB'
                    regio = 'YK'
                    counc = ''.

因此,我创建了一个工作区wa_t005e,其类型与t_t005e.

的行相同

我想先把内部table读入t_t005e工作区wa_t005e,再读到字段wa_upload-region.

以下是我正在进行的工作:

    DATA: wa_t005e TYPE t005e.

    LOOP AT t_t005e INTO wa_t005e.
      ASSIGN COMPONENT wa_t005e-regio OF STRUCTURE 
          wa_t005e TO <wa_upload-region>.
    ENDLOOP.

如何获取wa_t005e-regio的数据到字段wa_upload-region

至少在 7.40 之前的系统中,无法将 table 中仅一列的值直接读取到结构的一个字段中。如果你有 7.40 系统,你可以像这样使用 "table expression"

TRY.
    wa_upload-region = t_t005e[ land1 = 'GB' regio = 'YK' counc = '' ]-regio.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.

在旧系统中,您必须将整个 table 行读入 structure,然后您可以从中取出字段,如下所示:

READ TABLE t_t005e INTO wa_t005e WITH KEY land1 = 'GB' regio = 'YK' counc = ''.
wa_upload-region = wa_t005e-regio.

如果要使用ASSIGN and the like, you can do that too. First you would read the table line into a structure again (in this case a field symbol留在主题中)。然后将结构体需要的component/field赋值给一个单值字段符号

DATA: t_upload TYPE STANDARD TABLE OF ty_upload,
      t_t005e TYPE STANDARD TABLE OF t005e.

FIELD-SYMBOLS: <fs_upload> TYPE ty_upload,
               <fs_t005e>  TYPE t005e,
               <region>    TYPE regio.   " or type any

SELECT *
  FROM t005e
  INTO CORRESPONDING FIELDS OF TABLE t_t005e.

READ TABLE t_t005e ASSIGNING <fs_t005e> WITH KEY land1 = 'GB' regio = 'YK' counc = ''.
ASSIGN COMPONENT 'REGIO' OF STRUCTURE <fs_t005e> TO <region>.   " <---
*Other option: number of column
*ASSIGN COMPONENT 3 OF STRUCTURE <fs_t005e> TO <region>.

APPEND INITIAL LINE TO t_upload ASSIGNING <fs_upload>.
<fs_upload>-region = <region>.

WRITE <fs_upload>-region.

但是仅阅读 table 中的一个条目真的是您想要做的吗?您没有在 READ 语句中指定 t005e 的所有键。它只会 select 适合的第一行。