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: 

How to build dynamic select query

Former Member
0 Kudos

Using the ZCO_SETTLE_CHK-REC_FIELD and DISTRIBUTION_RULE-KONTY write a dynamic select single query <table>-<field name> e.g. <COAS>- <AUART> on the <table> e.g. <COAS>. If the DISTRIBUTION_RULE-KONTY is u2018ORu2019, then use DISTRIBUTION_RULE-AUFNR value in the where clause of the query to fetch the ZCO_SETTLE_CHK-REC_FIELD value maintained in the <table>-<field name>. If DISTRIBUTION_RULE-KONTY = u2018KSu2019 or u2018PRu2019 use DISTRIBUTION_RULE-KOSTL or DISTRIBUTION_RULE- PS_PSP_PNR respectively in the where clause to fetch the values.

For above how we can build dynamic query

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Refer the below code which helps to design a dynamic where condition.

IF NOT p1 IS INITIAL.
CLEAR : lv_p1_condition.
CONCATENATE 'F1' ' = ' '''' p1 '''' INTO
lv_p1_condition.
ENDIF.
 
IF NOT p2 IS INITIAL.
CLEAR : lv_p2_condition.
CONCATENATE 'F2' ' = ' '''' p2 '''' INTO
lv_p2_condition.
ENDIF.
 
IF NOT p3 IS INITIAL.
CLEAR : lv_p3_condition.
CONCATENATE 'F3' ' = ' '''' p3 '''' INTO
lv_p3_condition.
ENDIF.
 
 
IF NOT lv_p1_condition IS INITIAL.
CONCATENATE lv_p1_condition lv_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
 
IF NOT lv_p2_condition IS INITIAL.
IF lv_condition IS INITIAL.
CONCATENATE lv_p2_condition lv_condition
INTO lv_condition SEPARATED BY space.
ELSE.
CONCATENATE lv_condition 'AND' lv_p2_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
ENDIF.
 
IF NOT lv_p3_condition IS INITIAL.
IF lv_condition IS INITIAL.
CONCATENATE lv_p3_condition lv_condition
INTO lv_condition SEPARATED BY space.
ELSE.
CONCATENATE lv_condition 'AND' lv_p3_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
ENDIF.
 
 
SELECT * FROM link INTO wa
WHERE lv_condition .

1 REPLY 1

Former Member
0 Kudos

Hi,

Refer the below code which helps to design a dynamic where condition.

IF NOT p1 IS INITIAL.
CLEAR : lv_p1_condition.
CONCATENATE 'F1' ' = ' '''' p1 '''' INTO
lv_p1_condition.
ENDIF.
 
IF NOT p2 IS INITIAL.
CLEAR : lv_p2_condition.
CONCATENATE 'F2' ' = ' '''' p2 '''' INTO
lv_p2_condition.
ENDIF.
 
IF NOT p3 IS INITIAL.
CLEAR : lv_p3_condition.
CONCATENATE 'F3' ' = ' '''' p3 '''' INTO
lv_p3_condition.
ENDIF.
 
 
IF NOT lv_p1_condition IS INITIAL.
CONCATENATE lv_p1_condition lv_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
 
IF NOT lv_p2_condition IS INITIAL.
IF lv_condition IS INITIAL.
CONCATENATE lv_p2_condition lv_condition
INTO lv_condition SEPARATED BY space.
ELSE.
CONCATENATE lv_condition 'AND' lv_p2_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
ENDIF.
 
IF NOT lv_p3_condition IS INITIAL.
IF lv_condition IS INITIAL.
CONCATENATE lv_p3_condition lv_condition
INTO lv_condition SEPARATED BY space.
ELSE.
CONCATENATE lv_condition 'AND' lv_p3_condition
INTO lv_condition SEPARATED BY space.
ENDIF.
ENDIF.
 
 
SELECT * FROM link INTO wa
WHERE lv_condition .