2023 Oct 24 9:31 AM
Hi together,
I am trying to do a select query with the wildcard, but I want to use also the range. The idea is that I need to do a selection from table nach where the vakey field contains the data the user will enter, which may be several. Thus, one example of the code that I have written is as follows:
SELECT knumh FROM nach
INTO TABLE @DATA(lt_test)
WHERE kschl = 'ZZ05'
AND vakey LIKE '%507'.
The upper code works perfectly fine, but now I want to use the range option too. Thus for where condition in vakey, I want to use several options, like this:
DATA: lr_vakey TYPE RANGE OF vakey.
APPEND INITIAL LINE TO lr_vakey ASSIGNING FIELD-SYMBOL(<ls_vakey>).
IF <ls_vakey> IS ASSIGNED.
<ls_vakey>-low = |%{ ls_user-matnr }|.
<ls_vakey>-sign = 'I'.
<ls_vakey>-option = 'CP'.
APPEND <ls_vakey> TO lr_vakey.
ENDIF.
SELECT knumh FROM nach
INTO TABLE @DATA(lt_test)
WHERE kschl = 'Z305'
AND vakey IN @lr_vakey.
But when I use like this I have a sy-subrc of 4. I also tried with the option 'EQ', but it does not work.
Can anyone tell me how can I use a select query that has both the wildcard and range option?
Thank you in advance!
Best regards,
R
2023 Oct 24 9:43 AM
Hello, you can append it to range table like you are doing. And using I (include) with CP (contains pattern) is also correct. Only mistake you have is wrong wildcard character:
For CP, there are "*" and "+".
For LIKE there is "%" and "_".
Check documentation for CP operator:
https://help.sap.com/doc/abapdocu_731_index_htm/7.31/en-US/abenlogexp_strings.htm
Note that there might be a conflict of selection from user and yours!
2023 Oct 24 9:43 AM
Hello, you can append it to range table like you are doing. And using I (include) with CP (contains pattern) is also correct. Only mistake you have is wrong wildcard character:
For CP, there are "*" and "+".
For LIKE there is "%" and "_".
Check documentation for CP operator:
https://help.sap.com/doc/abapdocu_731_index_htm/7.31/en-US/abenlogexp_strings.htm
Note that there might be a conflict of selection from user and yours!
2023 Oct 24 11:06 AM
Many thanks, it worked 🙂
May I also ask you, what kind of conflict it may be? As normally I will just use the data that the user gives not my own.
2023 Oct 24 12:56 PM
tk_gerald consider user enters:
E CP *123 (exclude)
And you enter wih this append:
I CP *123 (include)
Now you have a conflict.
2023 Oct 24 9:55 AM
Since you are using a fairly recent version of ABAP, why not use the "new" syntax.
DATA range_of_vakey TYPE RANGE OF vakey.
range_of_vakey = VALUE #( ( sign = 'I' option = 'CP' low = |*{ user-matnr }|.
SELECT knumh FROM nach
INTO TABLE @DATA(lt_test)
WHERE kschl = 'Z305'
AND vakey IN @range_of_vakey.
(In any case, your program was wrong. You already have put the entry into your table, and then you append it again. If you debug your original you'll see lr_vakey contains two duplicate entries:
I CP %<matnr>
I CP %<matnr>.