2007 Aug 24 8:59 AM
Hi friends,
I have three inputs in my selection screen, I need to get all the possible condtitions and run the select querry.
Ex : a , b , c
if a
if a and b
if a and b and c
if b
if b and c
if c
if c and a
Please tell me how to write the if condition on this example.
Regards,
Line
2007 Aug 24 9:15 AM
HI
PARAMETERS : A TYPE I,
B TYPE I ,
C TYPE I.
START-OF-SELECTION.
IF NOT A IS INITIAL AND B IS INITIAL AND C IS INITIAL
WRITE : A.
ENDIF.
IF NOT A IS INITIAL AND NOT B IS INITIAL AND C IS INITIAL
WRITE : A,B.
ENDIF.
IF NOT A IS INITIAL AND NOT B IS INITIAL AND C IS NOT INITIAL
WRITE : A,B.C
ENDIF.
IF A IS INITIAL AND NOT B IS INITIAL AND C IS INITIAL
WRITE : B
ENDIF.
IF A IS INITIAL AND NOT B IS INITIAL AND C IS NOT INITIAL
WRITE : B,C.
ENDIF.
IF A IS INITIAL AND B IS INITIAL AND C IS NOT INITIAL
WRITE : C.
ENDIF.
IF NOT A IS INITIAL AND B IS INITIAL AND C IS NOT INITIAL
WRITE : A,C
ENDIF.
THIS IS THE CODE
REWARD IF USEFUL
2007 Aug 24 9:03 AM
Hi,
Use dynamic WHERE clause as follows.
DATA: str type string.
IF a is not initial. " Check if A is not initial
concatenate 'FLDA = ' a into str.
ENDIF.
if b is not initial. " check if B is not inital
if str is initial. " Check if str (Where clause is initial)
concatenate 'FLDB = ' b into str.
else.
concatenate str ' AND FLDB = ' b into str.
endif.
ENDIF.
if c is not initial. " check if C is not inital
if str is initial. " Check if str (Where clause is initial)
concatenate 'FLDC = ' c into str.
else.
concatenate str ' AND FLDC = ' c into str.
endif.
endif.
if str is not initial. "(Check if WHERE clause is not initial)
SELECT * FROM table INTO itab WHERE (str).
else.
SELECT * FROM table INTO itab.
endif
Regards,
Sesh
2007 Aug 24 9:05 AM
2007 Aug 24 9:08 AM
Hi,
Since you want to build your WHERE condition based on if A B C are supplied or only some of them are supplied or if none of them are supplied then you can ignore WHERE clause for select query.
So my code is actually creating a Dynamic WHERE clause so that you will get WHERE clause as per the data in A B and C.
Regards,
Sesh
2007 Aug 24 9:16 AM
Hi Sesh,
Your code is good I am trying to figure out how it executes, Thanx for helping, I would appreciate if you can send me any sample code for this.
Regards,
Line
2007 Aug 24 9:32 AM
Hi,
I tried but cannot workout, I have my fields like this
1. (no-low) and (no-high)
2. (date-low) and (date-high)
3. user
can any one please help me with the above mentioned field
Regards,
Line
2007 Aug 24 9:36 AM
Hi,
Check out the help for SELECT using Dynamic WHERE clauses under Keyword documentation of SELECT.
There you can find some sample code for dynamic WHERE clause.
If you could not then here is the sample code
PARAMETERS: column TYPE c LENGTH 8,
value TYPE c LENGTH 30.
DATA spfli_wa TYPE spfli.
DATA cond_syntax TYPE string.
CONCATENATE column '= value'
INTO cond_syntax SEPARATED BY space.
TRY.
SELECT SINGLE *
FROM spfli
INTO spfli_wa
WHERE (cond_syntax). " HERE cond_syntx is a string containing the WHERE clauses fields and values)
CATCH cx_sy_dynamic_osql_error.
MESSAGE `Wrong WHERE condition!` TYPE 'I'.
ENDTRY.
So in your case you need to check the values of A B and C and accordingly build the WHERE clause string as I have done in my code of first reply.
Regards,
Sesh
2007 Aug 24 9:06 AM
Hi Line,
you need to mention that all the (a,b,c) fields are from one table or differnet.
If it belongs to one table you can write in where condition. else you have to find the link between the table and you can fetch the values.
Reward if useful.
kishore
2007 Aug 24 9:11 AM
2007 Aug 24 9:15 AM
HI
PARAMETERS : A TYPE I,
B TYPE I ,
C TYPE I.
START-OF-SELECTION.
IF NOT A IS INITIAL AND B IS INITIAL AND C IS INITIAL
WRITE : A.
ENDIF.
IF NOT A IS INITIAL AND NOT B IS INITIAL AND C IS INITIAL
WRITE : A,B.
ENDIF.
IF NOT A IS INITIAL AND NOT B IS INITIAL AND C IS NOT INITIAL
WRITE : A,B.C
ENDIF.
IF A IS INITIAL AND NOT B IS INITIAL AND C IS INITIAL
WRITE : B
ENDIF.
IF A IS INITIAL AND NOT B IS INITIAL AND C IS NOT INITIAL
WRITE : B,C.
ENDIF.
IF A IS INITIAL AND B IS INITIAL AND C IS NOT INITIAL
WRITE : C.
ENDIF.
IF NOT A IS INITIAL AND B IS INITIAL AND C IS NOT INITIAL
WRITE : A,C
ENDIF.
THIS IS THE CODE
REWARD IF USEFUL