2006 Oct 29 8:35 AM
Hi,
How can we write dynamic where condition for an internal table as we can write READ statement as follows.
data : lv_key(10) type c value 'ANLKL'.
read table itab1 with key (lv_key) = 'ANLKL'.
Is it possible to write LOOP statement something like this?
Loop at itab where (lv_key) is initial.
endloop.
Any help or pointer is appreciated.
Thanks,
Deepak Bhalla
Hi,
How can we write dynamic where condition for an internal table as we can write READ statement as follows.
data : lv_key(10) type c value 'ANLKL'.
read table itab1 with key (lv_key) = 'ANLKL'.
Is it possible to write LOOP statement something like this?
Loop at itab where (lv_key) is initial.
endloop.
Any help or pointer is appreciated.
Thanks,
Deepak Bhalla
2006 Oct 29 9:59 AM
data : lv_key(10) type c value 'ANLKL',
lv_ANLKL(10) type c.
FIELD-SYMBOLS: <lv_key>.
ASSIGN (lv_key) TO <lv_key>.
lv_ANLKL = <lv_key>.
Loop at itab where lv_ANLKL is initial.
endloop.
2006 Oct 29 12:10 PM
shakeela,
this will give you a syntax error, because the WHERE condition for LOOP clause is incorrect:
The condition logexp can be almost any logical expression. The only restriction is that the first field for each comparison must be a sub-field of the line structure of the internal table itab.
It is true, that you can't specify it dynamically.
What you can do as an alternative, is to check all fields that could be used dynamically against ranges and to fill just the rabges you need.
Something like
<pre>
data:
lr_bukrs type range of bseg-bukrs,
lr_gjahr type range of bseg-gjahr,
lr_belnr type range of bseg-belnr.
filll range(s) dynamically ***
...
...
loop at itab where bukrs in lr_bukrs
and gjahr in lr_gjahr
and belnr in lr_belnr.
do something
endloop.
</pre>
Because a where-condition on an empty range is always true, empty ranges will be ignored and only the ranges with values will be used for comparison.
To fill a range, you may use
<pre>
PERFORM append_range
USING 'IEQ' '2006' ''
CHANGING lr_gjahr.
</pre>
using this universal reusable lightning-fast form
<pre>
&----
*& Form append_range
&----
append selection range
----
FORM append_range USING p_signopt TYPE c
p_low TYPE any
p_high TYPE any
CHANGING pt_range TYPE table.
FIELD-SYMBOLS:
<range> TYPE ANY,
<sign> TYPE ANY,
<option> TYPE ANY,
<low> TYPE ANY,
<high> TYPE ANY.
DATA:
l_ref TYPE REF TO data.
CREATE DATA l_ref LIKE LINE OF pt_range.
ASSIGN l_ref->* TO <range>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'SIGN' OF STRUCTURE <range> TO <sign>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'OPTION' OF STRUCTURE <range> TO <option>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'LOW' OF STRUCTURE <range> TO <low>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'HIGH' OF STRUCTURE <range> TO <high>.
CHECK sy-subrc = 0.
<sign> = p_signopt(1).
<option> = p_signopt+1(2).
<low> = p_low.
<high> = p_high.
READ TABLE pt_range WITH KEY table_line = <range>
TRANSPORTING NO FIELDS BINARY SEARCH.
CHECK sy-subrc <> 0.
INSERT <range> INTO pt_range INDEX sy-tabix..
ENDFORM. " append_range
</pre>
Regards,
Clemens
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |