英文:
Read internal table - field symbol has not been assigned yet
问题
我写了这段小代码,根据另一个内部表it_alv_to_check
中的值更新it_alv
中的数据。
在执行ELSE
语句块中的代码时,我遇到了错误:
字段符号尚未被分配
但是我最初使用READ TABLE
语句动态分配了它。
如何确保即使sy-subrc <> 0
,字段符号也会被分配?
FIELD-SYMBOLS: <to_check> LIKE LINE OF it_alv.
LOOP AT it_alv ASSIGNING FIELD-SYMBOL(<fs_alv_2>).
READ TABLE it_alv_to_check ASSIGNING <to_check> WITH KEY belnr = <fs_alv_2>-belnr.
IF sy-subrc = 0.
<fs_alv_2>-statement_year = <to_check>-statement_year.
<fs_alv_2>-laufi = <to_check>-laufi.
<fs_alv_2>-user_field_1 = <to_check>-user_field_1.
<fs_alv_2>-stceg = <to_check>-stceg.
<fs_alv_2>-name1 = <to_check>-name1.
<fs_alv_2>-monat_from = <to_check>-monat_from.
ELSE.
<fs_alv_2>-statement_year = <to_check>-statement_year.
<fs_alv_2>-laufi = <to_check>-laufi.
<fs_alv_2>-user_field_1 = <to_check>-user_field_1.
<fs_alv_2>-stceg = <to_check>-stceg.
<fs_alv_2>-name1 = <to_check>-name1.
<fs_alv_2>-monat_from = <to_check>-monat_from.
<fs_alv_2>-color = VALUE #( ( color-col = 6
color-int = 0
color-inv = 1
)
).
ENDIF.
ENDLOOP.
英文:
I have wrote this small piece of code which updates the data in it_alv
based on values in another internal table it_alv_to_check
.
At some point while executing the code in the ELSE
statement block, I got the error:
> field symbol has not been assigned yet
But I had it at first assigned dynamically using READ TABLE
statement.
How to make sure that even if sy-subrc <> 0
, the field symbol will be assigned?
FIELD-SYMBOLS: <to_check> LIKE LINE OF it_alv.
LOOP AT it_alv ASSIGNING FIELD-SYMBOL(<fs_alv_2>).
READ TABLE it_alv_to_check ASSIGNING <to_check> WITH KEY belnr = <fs_alv_2>-belnr.
IF sy-subrc = 0.
<fs_alv_2>-statement_year = <to_check>-statement_year.
<fs_alv_2>-laufi = <to_check>-laufi.
<fs_alv_2>-user_field_1 = <to_check>-user_field_1.
<fs_alv_2>-stceg = <to_check>-stceg.
<fs_alv_2>-name1 = <to_check>-name1.
<fs_alv_2>-monat_from = <to_check>-monat_from.
ELSE.
<fs_alv_2>-statement_year = <to_check>-statement_year.
<fs_alv_2>-laufi = <to_check>-laufi.
<fs_alv_2>-user_field_1 = <to_check>-user_field_1.
<fs_alv_2>-stceg = <to_check>-stceg.
<fs_alv_2>-name1 = <to_check>-name1.
<fs_alv_2>-monat_from = <to_check>-monat_from.
<fs_alv_2>-color = VALUE #( ( color-col = 6
color-int = 0
color-inv = 1
)
).
ENDIF.
ENDLOOP.
答案1
得分: 2
在这种情况下,你不能将字段符号分配给你的字段。你使用READ TABLE
语句读取一个内部表,如果找到了给定键的条目(即sy-subrc = 0
),则将找到的表行分配给字段符号,字段符号指向内存中的表行。
否则,如果没有找到表行,你就没有条目可以分配给字段符号,字段符号保持不变或初始状态。在这种情况下,你不能依赖它被分配(它可以被分配,但指向错误的条目)或假设分配本身(保持初始状态,因为之前没有被分配)。
更多信息:READ TABLE, result ... ASSIGNING
根据你想要实现的目标,你可以在else分支中分配一些默认值,或者实现其他逻辑来处理这种情况。
英文:
You cannot have your field-symbol assigned in this scenario. You read an internal table using READ TABLE
statement and in case the entry was found with the given key (i.e. sy-subrc = 0
), the found table row is assigned to a field symbol and the field symbol points to the table row in the memory.
Otherwise, if no table row is found, you have no entry to assign field-symbol to, and the field symbol remains unchanged or initial. In this case you cannot rely on it being assigned (it can be assigned but point to the wrong entry) or assume the assignment itself (remains initial because it was not previously assigned).
More about it: READ TABLE, result ... ASSIGNING
Depending on what you want to achieve you can assign some default values in the else branch or implement some other logic to handle this scenario.
答案2
得分: 0
你的代码似乎在做以下事情:
- 如果在 it_alv_to_check 中找到对应的行,将该行的值覆盖到 it_alv 中的各个值上。
- 如果在 it_alv_to_check 中找不到对应的行,那么也将那些值(来自不存在的行!)写入 it_alv,并将颜色设置为固定值。
第二点的第一部分显然是不可能的。你不能对不存在的数据进行操作。从基本逻辑上讲,这是没有意义的。
所以你首先需要做一些概念性的工作。当 it_alv_to_check 不包含与 belnr 对应的行时,你希望在 it_alv
的行中的 statement_year、laufi、user_field_1、stceg、name1 和 monat_from 这些值发生什么变化?保持原样?用固定的占位数据填充?用不同行的值填充,如果是的话,是哪一行的值?
回答这个问题(可能需要与给你这个要求的人一起讨论),你实际问题的答案可能会自然而然地出现。如果没有,可以随时提出一个新问题,描述你希望发生的具体情况以及你需要知道的内容。
英文:
What your code appears to be doing is:
- If a corresponding row is found it_alv_to_check, take the values of that row to overwrite various values in it_alv.
- If no corresponding row is found in it_alv_to_check, then also take those values (from the row that does not exist!) and write them into it_alv. And also set the color to a fixed value.
The first part of the second point is obviously impossible. You can not do something with data which does not exist. This just does not make sense on a basic logical level.
So what you need to do here first is some conceptual work. What exactly do you expect to happen with the values statement_year, laufi, user_field_1, stceg, name1 and monat_from in the row of it_alv
when it_alv_to_check
does not contain a row for that belnr
? Leave them as they are? Fill them with fixed placeholder data? Fill them with values from a different row, and if so which one?
Answer this question (probably with the help of the person who gave you this requirement) and the answer to your actual question will probably appear on its own. If not, feel free to ask a new question where you describe what exactly you want to happen and what you need to know in order to do that.
答案3
得分: -2
我已经将字段符号的声明更改为:
READ TABLE it_alv_to_check ASSIGNING FIELD SYMBOL(<to_check>) WITH KEY belnr = <fs_alv_2>-belnr.
现在它正常工作了。
英文:
I've changed declaring of the field symbol to:
READ TABLE it_alv_to_check ASSIGNING FIELD SYMBOL(<to_check>) WITH KEY belnr = <fs_alv_2>-belnr.
and now it works fine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论