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: 

Dynamic where clause with several conditions

Hi everybody,

I'm a newbie with dynamic programming and I need some help with an recent issue.

I'm trying to add a dynamic where clause to a select in this way,

DATA : tab(100) OCCURS 0 WITH HEADER LINE.

APPEND 'FIELD = FIELD1' TO tab.
APPEND ' AND ' TO tab.
APPEND 'FIELD TO FIELD2' TO tab.

SELECT SINGLE *
FROM (p_tabla)
INTO <struc>
WHERE (tab).

But it only works with one condition, if I add more than one I get a sy-subrc = 4.

Is there another way of adding several conditions statements to a "where" clause dynamically?

Thanks a lot!!

1 ACCEPTED SOLUTION

raviandela
Explorer

I think you should use clike data for dynamic where condition.

Try this.

data: lv_where type text100.

Concatenate 'BUKRS = P_BUKRS'  'AND' 'BUDAT IN S_BUDAT' into lv_where separated by space.

SELECT SINGLE *
 FROM (p_tabla)
 INTO <estructura>
 WHERE (lv_where).
7 REPLIES 7

franois_henrotte
Active Contributor
0 Kudos

1) APPEND 'FIELD TO FIELD2' TO tab. should be APPEND 'FIELD = FIELD2' TO tab.

2) if the same field is used then you should use an OR statement as an AND will prevent from finding any record

3) be sure to put values into quotation marks if the field is alphanumeric

note: it is not mandatory to do it in a table, it could be a string also

0 Kudos

Thanks a lot for your answer, I've tried the following

APPEND 'BUKRS = P_BUKRS' TO tab.
append ' AND ' to tab.
append 'BUDAT IN S_BUDAT' to tab.

SELECT SINGLE *
FROM (p_tabla)
INTO <estructura>
WHERE (tab).

And still get the same error.... could it be for the type of the tab table?

Thank you so much

DoanManhQuynh
Active Contributor
0 Kudos

I think you should check are there data fit with your sql or not, since i dont see your dynamic have any mistake.

raviandela
Explorer

I think you should use clike data for dynamic where condition.

Try this.

data: lv_where type text100.

Concatenate 'BUKRS = P_BUKRS'  'AND' 'BUDAT IN S_BUDAT' into lv_where separated by space.

SELECT SINGLE *
 FROM (p_tabla)
 INTO <estructura>
 WHERE (lv_where).

0 Kudos

Thanks a lot, it was solved with your answer, but adding a space after the AND, like 'AND '.

matt
Active Contributor
DATA : tab(100) OCCURS 0 WITH HEADER LINE.

Tables with header lines have been obsolete for more than 15 years. Please do not use them.

former_member220028
Active Contributor

maybe this Fm fits ur requirement

CRS_CREATE_WHERE_CONDITION