‎2004 Dec 23 7:41 PM
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.
‎2004 Dec 23 8: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
http://help.sap.com/saphelp_47x200/helpdata/en/67/93b80914a911d2953c0000e8353423/frameset.htm
Just look at dynamic conditions
‎2004 Dec 23 9:46 PM
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
‎2004 Dec 23 10:06 PM
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.