我的目标是使用FOR
循环实现以下目标。
DATA(lt_wherecondn) = VALUE string_table( ).
DATA(lt_fields) = VALUE rsds_frange_t(
( fieldname = 'A~FLD1' ) ( fieldname = 'A~FLD2' ) ).
DATA(lv_lines) = lines( lt_fields ).
LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<fields>).
DATA(lv_length) = strlen( <fields>-fieldname ) - 2.
DATA(lv_line) = COND string(
WHEN lv_lines <> sy-tabix
THEN <fields>-fieldname+2(lv_length) && | IN | && |@| && <fields>-fieldname && | AND |
ELSE <fields>-fieldname+2(lv_length) && | IN | && |@| && <fields>-fieldname ).
APPEND lv_line TO lt_wherecondn.
ENDLOOP.
还有:
LT_FIELDS
中有字段名- 将
LT_FIELDS-FIELDNAME
数据连接为特定格式 - 将其附加到
LT_WHERE
LT_WHERECONDN
:中的结果
FLD1 IN @A~FLD1 AND
FLD2 IN @A~FLD2
以下是我的代码(不确定在循环中添加lv_length
逻辑的位置(:
TYPES: BEGIN OF ty_whr,
fieldname TYPE string,
END OF ty_whr.
DATA(lt_where) = REDUCE ty_whr(
INIT whereclause = VALUE ty_whr( )
FOR <fields> IN lt_fields
NEXT whereclause-fieldname =
COND #( WHEN whereclause IS NOT INITIAL
THEN <fields>-fieldname && | IN | && |@| && <fields>-fieldname && | AND |
ELSE <fields>-fieldname && | IN | && |@| && <fields>-fieldname ) ).
上面的代码片段使用fieldname
创建了一个深层结构lt_where
,并且只保存一行数据。似乎是语法问题。这里需要更正什么?
如果你不介意,我更愿意首先发布构造函数表达式,它与你的经典循环完全等效,下面是我的备注:
- 如果不需要复杂的计算,则首选
VALUE
而不是REDUCE
- 使用
LET var1 = expr1 var2 = expr2 IN
计算辅助变量(只能在此表达式中使用( - 下面的代码完全反映了您原来的循环,而不是为了简化答案而引入的新结构(
ty_whr
(
完整代码:
DATA(lt_fields) = VALUE rsds_frange_t(
( fieldname = 'A~FLD1' ) ( fieldname = 'A~FLD2' ) ).
DATA(lv_lines) = lines( lt_fields ).
DATA(lt_wherecondn) = VALUE string_table(
FOR <fields> IN lt_fields INDEX INTO lv_line
LET lv_length = strlen( <fields>-fieldname ) - 2 IN
( COND #( WHEN lv_lines <> lv_line
THEN |{ <fields>-fieldname+2(lv_length) } IN @{ <fields>-fieldname } AND |
ELSE |{ <fields>-fieldname+2(lv_length) } IN @{ <fields>-fieldname }| ) ) ).
仅供参考,
- 关于具有不同结构和新逻辑的特定需求,以下是您可以获得的内容(与以前的代码相比没有什么特别之处,只是添加了表类型
ty_whr_lines
(:
TYPES: BEGIN OF ty_whr,
fieldname TYPE string,
END OF ty_whr,
ty_whr_lines TYPE STANDARD TABLE OF ty_whr WITH EMPTY KEY.
DATA(lt_fields) = VALUE rsds_frange_t(
( fieldname = 'A~FLD1' ) ( fieldname = 'A~FLD2' ) ).
DATA(lv_lines) = lines( lt_fields ).
DATA(lt_where) = VALUE ty_whr_lines(
FOR <fields> IN lt_fields INDEX INTO lv_line
( fieldname = COND #( WHEN lv_lines <> lv_line
THEN |{ <fields>-fieldname } IN @{ <fields>-fieldname } AND |
ELSE |{ <fields>-fieldname } IN @{ <fields>-fieldname }| ) ) ).
- 关于
REDUCE
,如果您愿意,可以使用它,但它更复杂:
TYPES: BEGIN OF ty_whr,
fieldname TYPE string,
END OF ty_whr,
ty_whr_lines TYPE STANDARD TABLE OF ty_whr WITH EMPTY KEY.
DATA(lt_fields) = VALUE rsds_frange_t(
( fieldname = 'A~FLD1' ) ( fieldname = 'A~FLD2' ) ).
DATA(lv_lines) = lines( lt_fields ).
DATA(lt_where) = REDUCE #(
INIT whereclause = VALUE ty_whr_lines( )
FOR <fields> IN lt_fields
NEXT whereclause = VALUE #( BASE whereclause
( fieldname = COND #( WHEN whereclause IS NOT INITIAL
THEN |{ <fields>-fieldname } IN @{ <fields>-fieldname } AND |
ELSE |{ <fields>-fieldname } IN @{ <fields>-fieldname }| ) ) ) ).