为什么类型不兼容?

时间:2019-05-15 10:12:26

标签: abap

我有一个3维表(带嵌套表的表),我的目标是删除内部表中具有FIELDNAME等于'SCAN_ID'的行:

TYPES : BEGIN OF ty_cell,
          fieldname TYPE lvc_fname,
        END OF ty_cell,
        ty_celltab TYPE STANDARD TABLE OF ty_cell WITH EMPTY KEY.
TYPES : BEGIN OF ty_line,
          celltab TYPE ty_celltab,
        END OF ty_line,
        zatool_t_doc_input TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.

DATA(it_doc_input) = VALUE zatool_t_doc_input( ( celltab = VALUE #( ( fieldname = 'SCAN_ID' ) ) ) ).

DATA(rt_doc_input) = REDUCE zatool_t_doc_input( " <=== line of syntax error
      INIT lt_doc_input = VALUE zatool_t_doc_input( )
      FOR ls_doc_input IN it_doc_input
      NEXT lt_doc_input = VALUE #( 
            BASE ls_doc_input 
            celltab = FILTER #( 
                  ls_doc_input-celltab 
                  WHERE fieldname <> CONV lvc_fname( 'SCAN_ID' ) ) ) ).

编译器给出此语法错误:

  

“ LS_DOC_INPUT”的类型不能转换为“ LT_DOC_INPUT”的类型

错误在哪里?

1 个答案:

答案 0 :(得分:3)

使用如下所示的“表格理解”,您可以获得带有过滤的单元格标签的表格:

TYPES:
  BEGIN OF zatool_t_doc_input_s,
    field1  TYPE c,
    field2  TYPE c,
    celltab TYPE lvc_t_styl,
  END OF zatool_t_doc_input_s,
  zatool_t_doc_input TYPE STANDARD TABLE OF zatool_t_doc_input_s WITH EMPTY KEY.

DATA(it_doc_input) = VALUE zatool_t_doc_input(
                      ( field1 = 'A' field2 = 'B' celltab = VALUE #( ( fieldname = 'SCAN_ID' ) ) )
                      ( field1 = 'C' field2 = 'D' celltab = VALUE #( ( fieldname = 'USER_ID' ) ) )
                      ( field1 = 'C' field2 = 'D' celltab = VALUE #( ( fieldname = 'SCAN_ID' ) ) )
                      ( field1 = 'E' field2 = 'F' celltab = VALUE #( ( fieldname = 'SYST_ID' ) ) )
                     ).

DATA(rt_doc_input) =  VALUE zatool_t_doc_input(
                      FOR ls_doc_input IN it_doc_input (
                        VALUE #( BASE ls_doc_input
                                 celltab = FILTER #( ls_doc_input-celltab
                                           WHERE fieldname <> CONV lvc_fname( 'SCAN_ID' ) ) ) ) ).

关于语法错误,正如@Florian指出的那样,问题是当将BASE与结构(在这种情况下为ls_doc_input)一起使用时,相应的VALUE #(返回一个结构。如果VALUE要返回表,则BASE还必须使用表。

尽管如此,即使您绕过BASE语法错误

DATA(rt_doc_input) =
  REDUCE #( INIT lt_doc_input = VALUE zatool_t_doc_input( )
            FOR ls_doc_input IN it_doc_input
            NEXT lt_doc_input = VALUE #( ( field1  = ls_doc_input-field1
                                           field2  = ls_doc_input-field2
                                           celltab = FILTER #( ls_doc_input-celltab
                                                     WHERE fieldname <> CONV lvc_fname( 'SCAN_ID' ) ) 
                                       ) ) 
           ). "reduces the original table to a single line (albeit with celltab filtered)

甚至是等效方法,使用BASE避免单独的字段分配

DATA(rt_doc_input) =
  REDUCE #( INIT lt_doc_input = VALUE zatool_t_doc_input( )
            FOR ls_doc_input IN it_doc_input
            NEXT lt_doc_input =
              VALUE #(
                ( VALUE #( BASE ls_doc_input
                           celltab = FILTER #( ls_doc_input-celltab
                                     WHERE fieldname <> CONV lvc_fname( 'SCAN_ID' ) )
                          ) "VALUE: table line using BASE
                ) " VALUE: table row
              ) "VALUE: table
           ). "reduces the original table to a single line (albeit with celltab filtered)

它仍然无法满足我的目标,因为它将it_doc_input减少到一行。