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

ABAP Query Question

Former Member
0 Likes
558

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?

Like

a = '1'

b = ''

c = '2'

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

Any ideas?

thanks, Graeme.

3 REPLIES 3
Read only

Former Member
0 Likes
513

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

http://help.sap.com/saphelp_47x200/helpdata/en/67/93b80914a911d2953c0000e8353423/frameset.htm

Just look at dynamic conditions

Read only

0 Likes
513

If anyone would have a simple example that would help alot. That help is pretty confusing (I'm far from an ABAP guru mind you )

thanks! graeme

Read only

0 Likes
513

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).

endselect.

Note that you can combine static and dynamic where clauses:

select * from zcostfreq

where bukrs = '2021' AND

(where_tab).

endselect.