Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Select query with wildcard and range option

Raxph
Participant
0 Kudos
2,706

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

1 ACCEPTED SOLUTION

Tomas_Buryanek
Active Contributor
2,618

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!

-- Tomas --
4 REPLIES 4

Tomas_Buryanek
Active Contributor
2,619

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!

-- Tomas --

0 Kudos
2,618

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.

0 Kudos
2,618

tk_gerald consider user enters:

E CP *123 (exclude)

And you enter wih this append:

I CP *123 (include)

Now you have a conflict.

-- Tomas --

matt
Active Contributor
2,618

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>.