Application Development and Automation 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: 
Read only

Dynamic WHERE condition

Former Member
0 Likes
847

Hi Gurus,

I have to select records from <TABLE > with condition <fieldname> EQ 'X'.

<fieldname> will be from selection screen parameter value <P_fieldname>.

How can i make a where condition?

my where condition shoule be like WHERE <P_FIELDNAME> EQ 'X'.

PLS HELP...

7 REPLIES 7
Read only

Former Member
0 Likes
786

Hi,

<b>select * from <table>

into <inttable>

where <p_fieldname> eq 'X'.

</b>

Read only

Former Member
0 Likes
786

Try understanding this code ..


 select * from <dbtable>
 into itab 
where <p_fldname> eq 'X'.

Read only

0 Likes
786

This code will not work...

Read only

Former Member
0 Likes
786

hi,

If you are already working an 6.40 (or higher) than you can simply concatenate the WHERE condition into a string and code:

DATA:

gd_where_conditions TYPE string.

SELECT * FROM ...

WHERE ( gd_where_conditions ).

However, if you are working on 6.20 (or less) than you could use the following function module to create your dynamic WHERE condition(s): RH_DYNAMIC_WHERE_BUILD

The function module returns the WHERE conditions in CONDTAB. Thus, you could code:

TYPES: BEGIN OF ty_s_clause.

TYPES: line(72) TYPE c.

TYPES: END OF ty_s_clause.

DATA:

gt_where_clauses TYPE STANDARD TABLE OF ty_s_clause

WITH DEFAULT KEY.

DATA:

gt_condtab TYPE STANDARD TABLE OF hrcond.

  • Fill gt_condtab with the required conditions

CALL FUNCTION 'RH_DYNAMIC_WHERE_BUILD'

EXPORTING

dbtable = space " can be empty

TABLES

condtab = gt_condtab

where_clause = gt_where_clauses

EXCEPTIONS

empty_condtab = 01

no_db_field = 02

unknown_db = 03

wrong_condition = 04.

  • Select your data

SELECT * FROM ...

WHERE (gt_where_clauses).

hope this helps.

plz reward if helpful

Read only

Former Member
0 Likes
786

hi,

parameters : p_field like <type>.

select * from <table>

into <itab>

where <p_field> = 'X'.

Read only

Former Member
0 Likes
786

hi,

parameters : p_field like <type>.

select * from <table>

into <itab>

where <p_field> = 'X'.

Read only

Former Member
0 Likes
786
Syntax wise this code is working but its giving dump while executing , i guess it is not possible

REPORT abc.

TABLES : mara.

PARAMETERS : p_field LIKE mara-matnr,
             p_tab LIKE dd03l-tabname.

FIELD-SYMBOLS: <fs> TYPE ANY.

ASSIGN p_field TO <fs>.

SELECT SINGLE matnr FROM (p_tab) INTO mara-matnr WHERE <fs> = '000000000000001518'.

WRITE : mara-matnr.