Skip to Content
author's profile photo Former Member
Former Member

ABAP Query Question

Hello. Is there a way to dynamically generate a query?

For example: I'm doing an RFC call that sometimes the strings contains data, the other times they don't. Is there a way of building the query to only 'and' the values when there are values there?


a = '1'

b = ''

c = '2'

select * from whereever where a = 1 (dont include b because its empty) and c = 2

Any ideas?

thanks, Graeme.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Dec 23, 2004 at 08:50 PM

    Yes, you can. It can be restrictive. I've done it, though I don't have any examples to hand. Dug up the help for it

    Just look at dynamic conditions

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Heres a good solution I found:

      Dynamic where clauses in open SQL

      You can use an internal table to build a dynamic where clause:

      data: where_tab(30) occurs 1 with header line,

      where_clause(30) type c.

      • Build the where clause. Will look like this when finished

      • WHERE ZAFSTMD02 = 'X' AND rbusa = '5145'

      • With a constant, result: ZAFSTMD01 = 'X'

      concatenate 'ZAFSTMD' zcostcheck-zmaaned ' = ''X''' into where_clause.

      • Append to internal table where_tab

      append where_clause to where_tab.

      • With a variable, result: AND rbusa = '5145'

      concatenate 'AND rbusa = ' '''' i_tab-zgsber ''''

      append where_clause to where_tab.

      • Select

      select * from zcostfreq

      where (where_tab).


      Note that you can combine static and dynamic where clauses:

      select * from zcostfreq

      where bukrs = '2021' AND



Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.