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 in Select statement

Former Member
59,758

Hi,

I have 10 fields on selection-screeen. In which ever field the user enters single values or ranges,i should pick that field dynamically and pass that field along with value range to Where condition of Select statement.How can i achieve this? Please help.

Regards

K Srinivas

1 ACCEPTED SOLUTION
Read only

Former Member
21,468

see the following example:


data : begin of itab occurs 0,
         matnr like mara-matnr,
end of itab.

ypes: begin of ty_s_clause.
types:   line(72)  type c.
types: end of ty_s_clause.

data : begin of gt_condtab occurs 0.
        include structure hrcond.
data : end   of gt_condtab.

FIELD-SYMBOLS <fs_wherecond> TYPE ty_s_clause.
data:
  gt_where_clauses  type standard table of ty_s_clause
                    with default key.


gt_condtab-field = 'MATNR'.
gt_condtab-opera = 'EQ'.
gt_condtab-low = '000000000000000111'.
append  gt_condtab.
clear  gt_condtab.

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 matnr from mara into table itab where (gt_where_clauses).

7 REPLIES 7
Read only

Former Member
0 Likes
21,468

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 matnr from mara into table itab where (gt_where_clauses).
Read only

0 Likes
21,468

Thanks for the reply.How the entries should be populated to gt_condtab and gt_where_clauses.When im executing the FM,entering db name im not getting values in gt_condtab and gt_where_clauses. Pls help.

Regards

K Srinivas

Read only

0 Likes
21,468

Hi,

Simply check the fields whether they are populated or not and build the where clause.

[Dynamic Where Clause|http://www.howforge.com/dynamic-where-clause-in-abap-4]

Read only

Former Member
21,469

see the following example:


data : begin of itab occurs 0,
         matnr like mara-matnr,
end of itab.

ypes: begin of ty_s_clause.
types:   line(72)  type c.
types: end of ty_s_clause.

data : begin of gt_condtab occurs 0.
        include structure hrcond.
data : end   of gt_condtab.

FIELD-SYMBOLS <fs_wherecond> TYPE ty_s_clause.
data:
  gt_where_clauses  type standard table of ty_s_clause
                    with default key.


gt_condtab-field = 'MATNR'.
gt_condtab-opera = 'EQ'.
gt_condtab-low = '000000000000000111'.
append  gt_condtab.
clear  gt_condtab.

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 matnr from mara into table itab where (gt_where_clauses).

Read only

21,468

We can use a simple STRING concatenated also. Example:

DATA : l_where TYPE string,
       i_marc TYPE STANDARD TABLE OF marc.

CONSTANTS: l_quote TYPE char1 VALUE ''''.

PARAMETERS : p_plant TYPE marc-werks.

CONCATENATE 'WERKS' space '=' space l_quote p_plant l_quote space
            'AND' space 'PERKZ' space '=' space l_quote 'M' l_quote
       INTO l_where RESPECTING BLANKS.

SELECT * FROM marc INTO TABLE i_marc WHERE (l_where).

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
21,468

Hello,

Are the screen elements PARAMETERS or SELECT-OPTIONS?

If they are all SELECT-OPTIONS you can use all the elements in the WHERE clause of the SELECT statement e.g.,

SELECT field1 field2 field3
FROM dbtable
INTO TABLE itab
WHERE
field1 = s_elem1 AND
field2 = s_elem2 AND
field3 = s_elem3 .... so on & so forth

BR,

Suhas

Read only

0 Likes
21,468

I also have a strange feeling that the IN operator has been re-invented in this thread...just slightly more complicated.

Thomas