如何迭代string_table并修改行?动态SELECT字段别名添加异常排查
I see the issue you're facing—your attempt to transform the field list with aliases is resulting in an empty table instead of the expected ['ref~PERNR','ref~SUBTY']. Let's break down reliable fixes for this problem:
Why Might the Original Code Fail?
While the VALUE constructor with a FOR loop looks syntactically correct, there can be edge cases in how ABAP evaluates the right-hand side when assigning back to the same variable (especially in older runtime versions). Alternatively, if your ABAP version predates 7.40 SP05, the FOR loop in VALUE constructor syntax isn't supported at all, leading to an empty table.
Solution 1: Modify Elements In-Place with a Loop
This is the most straightforward and reliable approach, working across all modern ABAP versions. Instead of reassigning the entire table, loop through each element and update it directly:
REPORT ZITER_TEST. CONSTANTS lc_alias TYPE string VALUE 'ref'. DATA lt_field TYPE string_table. lt_field = VALUE string_table( ( 'PERNR' ) ( 'SUBTY' ) ). " Initial values: ['PERNR','SUBTY'] LOOP AT lt_field ASSIGNING FIELD-SYMBOL(<fs_field>). <fs_field> = lc_alias && `~` && <fs_field>. ENDLOOP. " Result: lt_field now contains ['ref~PERNR','ref~SUBTY']
Solution 2: Use a Temporary Variable for Transformation
If you prefer the VALUE constructor pattern, use a temporary table to hold the transformed values before overwriting the original. This avoids any potential evaluation conflicts:
REPORT ZITER_TEST. CONSTANTS lc_alias TYPE string VALUE 'ref'. DATA lt_field TYPE string_table. DATA lt_transformed TYPE string_table. lt_field = VALUE string_table( ( 'PERNR' ) ( 'SUBTY' ) ). lt_transformed = VALUE string_table( FOR <lv_tmp> IN lt_field ( lc_alias && `~` && <lv_tmp> ) ). lt_field = lt_transformed. " Result: lt_field now has the expected aliased values
Solution 3: Verify ABAP Version Support
If you want to stick with your original syntax, confirm your system is running ABAP 7.40 SP05 or newer. Older versions don't support the FOR loop inside the VALUE constructor, which would explain the empty table result.
内容的提问来源于stack exchange,提问作者Dorad

