I'm attempting to write a program that will grab the content from fields from a table both specified by the user on the selection screen.
For example, the user could specify the fields equnr
, b_werk
, b_lager
from the table eqbs
.
I've been able to accomplish this like so:
" Determine list of fields provided by user
DATA(lv_fields) = COND string(
WHEN p_key3 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|{ p_key1 }, { p_key2 }, { p_key3 }, { p_string }|
WHEN p_key2 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|{ p_key1 }, { p_key2 }, { p_string }|
WHEN p_key2 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|{ p_key1 }, { p_string }| ).
DATA: lv_field_tab TYPE TABLE OF line.
APPEND lv_fields TO lv_field_tab.
" Determine table specified by user and prepare for Open SQL query
DATA t_ref TYPE REF TO data.
FIELD-SYMBOLS: <t> TYPE any,
<comp> TYPE any.
CREATE DATA t_ref TYPE (p_table).
ASSIGN t_ref->* TO <t>.
ASSIGN COMPONENT lv_fields OF STRUCTURE <t> TO <comp>.
" Prepare result container
DATA: lt_zca_str_to_char TYPE TABLE OF zca_str_to_char,
ls_zca_str_to_char TYPE zca_str_to_char.
SELECT (lv_field_tab) FROM (p_table) INTO (@ls_zca_str_to_char-key1, @ls_zca_str_to_char-key2, @ls_zca_str_to_char-key3, @ls_zca_str_to_char-string).
APPEND ls_zca_str_to_char TO lt_zca_str_to_char.
ENDSELECT.
This will correctly populate lt_zca_str_to_char
with data from the table specified by the user.
However, this implies that the user is always providing p_key1
, p_key2
, and p_key3
. I could perform a different selection statement based on how many key fields the user provides, but what's the fun in that?
I set out to solve this like this:
DATA(lv_results) = COND string(
WHEN p_key3 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|(@ls_zca_str_to_char-key1, @ls_zca_str_to_char-key2, @ls_zca_str_to_char-key3, @ls_zca_str_to_char-string)|
WHEN p_key2 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|(@ls_zca_str_to_char-key1, @ls_zca_str_to_char-key2, @ls_zca_str_to_char-string)|
WHEN p_key2 IS NOT INITIAL AND p_string IS NOT INITIAL THEN
|(@ls_zca_str_to_char-key1, @ls_zca_str_to_char-string)| ).
SELECT (lv_field_tab) FROM (p_table) INTO (@lv_results).
APPEND ls_zca_str_to_char TO lt_zca_str_to_char.
ENDSELECT.
This will activate, and when I get to my Open SQL query (from a Z table, only filling out the first two of three possible key fields), the values are the following:
lv_field_tab
= GUID, TEXT_ID, TEXT_DATA
(Good)
p_table
= ZCR_TRANS_TEXT
(Good)
lv_results
= (@ls_zca_str_to_char-key1, @ls_zca_str_to_char-key2, @ls_zca_str_to_char-string)
(Good, 3 = 3!)
But, since I'm assuming the compiler is seeing (@lv_results)
as one single variable, the program dumps with the following error:
The current ABAP program attempted to execute an Open SQL statement containing a dynamic entry. The parser returned the following error: "The field list and the INTO list must have the same number of elements."
Is it possible for me to use the new Open SQL syntax to accomplish my dynamic INTO clause in harmony with my dynamic field list?