2009 Dec 02 1:15 PM
Hi experts,
I am trying to do a read on an internal table using field symbols of type any table. To be able to read more than one row at once, I'd like to read it into another internal table (instead of just one line and instead of looping through them one by one).
So far the following line rendered an error that the target internal table doesn't have a header line
read table <fs_v_tab> WITH KEY (<fs_comp>) = <fs_param>-low INTO <fs_v_tab>.(also attempted using "<fs_v_tab>->*" & "<fs_v_tab>[]" to be silly)
Based on the following article, I was wondering if it were possible to dynamicly create an internal table with header line.
[http://help-abap.blogspot.com/2008/09/dynamic-internal-table-creation.html|http://help-abap.blogspot.com/2008/09/dynamic-internal-table-creation.html]
Any help or tips on how to dynamicly filter an internal table field-symbol is greatly appreciated!
2009 Dec 02 1:46 PM
Just read the online help:
This statement reads a row from internal table itab.
Or more exact:
This statement reads one row from internal table itab.
Therefore you can not read multiple lines into another table. The header line is needed to put the result in. And this is not what you intended, the result will be in the header line, not in the table itself.
If the row to be read is not uniquely specified, the first suitable row is read. In the case of index tables, this row has the lowest table index of all matching rows.
At least you have to do what you dont wanna do: looping.
Hi
I don't think it can create an internal table with header line dynamically, but it can create a structure as work area separatly:
read table <fs_v_tab> assigning <wa_tab> WITH KEY (<fs_comp>) = <fs_param>-low.
Max
2009 Dec 02 1:46 PM
Just read the online help:
This statement reads a row from internal table itab.
Or more exact:
This statement reads one row from internal table itab.
Therefore you can not read multiple lines into another table. The header line is needed to put the result in. And this is not what you intended, the result will be in the header line, not in the table itself.
If the row to be read is not uniquely specified, the first suitable row is read. In the case of index tables, this row has the lowest table index of all matching rows.
At least you have to do what you dont wanna do: looping.
2009 Dec 02 2:35 PM
Ok, I figured out some workaround. As I don't want to loop through all lines (as there could be a thousand or more) and check each component:
For each condition:
- "Read table" to fetch the first match in table A
- Save the first match in a dynamicly created internal table (table B)
- Delete the row in table A
- Copy table B to table A
- Clear table B
- Repeat from the first step
I figure "read table" is a bit more performant than doing a loop for all lines and 'manually' checking with field symbols of type any whether the line matches all the conditions.
Any post scriptum thoughts welcome
2009 Dec 02 2:49 PM
Hi
If you need the field name to be used in "WITH KEY" conditions for READ TABLE statament, a code like this can be used:
DATA: BEGIN OF ITAB OCCURS 0,
FIELD,
END OF ITAB.
DATA: WHERE_FIELD(30) TYPE C.
PARAMETERS P TYPE C.
FIELD-SYMBOLS: <FS_TAB> TYPE TABLE,
<FS_WA> TYPE ANY.
START-OF-SELECTION.
DO 9 TIMES.
MOVE SY-INDEX TO ITAB-FIELD.
APPEND ITAB.
ENDDO.
ASSIGN ITAB[] TO <FS_TAB>.
WHERE_FIELD = 'FIELD'.
READ TABLE <FS_TAB> ASSIGNING <FS_WA> WITH KEY (WHERE_FIELD) = P.
IF SY-SUBRC = 0.
WRITE <FS_WA>.
ENDIF.I hope it can help you
Max
2009 Dec 02 2:59 PM
Thanks for your reply
I already noticed the only way to dynamicly specify a component is to use the "with key" syntax. I hope the next abap release will bring more flexibility.
A where doesn't support dynamic components as far as I'm aware. I believe a syntax error occurs if it can't check whether the left side of the condition is part of the table structure.
2009 Dec 02 3:16 PM
Hi
Yes I know....a dynamic WHERE conditions can be use in SELECT statament only, this is an old problem, I don't know if SAP thinks to solve it in the future.
Max
2009 Dec 02 3:30 PM
Looping with field symbols as shown by some examples isnt so horrible/bad performant.
2009 Dec 02 3:42 PM
Hi
Looping a table by field-symbol is not the problem for the performance.
Here the problem is to create the where condition dynamically: it's not possible with LOOP statament, so if it needs to use it, it has to insert a CHECK statament into the LOOP: this can worse the performance.
READ TABLE statament can have a dynamic conditions in the WITH KEY options, but READ TABLE can extract a single record, so it can't often be used.
Max
2009 Dec 02 3:48 PM
Hello Max,
A couple of months back i have read in this forum that the latest ABAP release will be supporting dynamic WHERE with the LOOP stmt
Cheers,
Suhas
2009 Dec 02 1:56 PM
2009 Dec 02 2:15 PM
Hi
I don't think it can create an internal table with header line dynamically, but it can create a structure as work area separatly:
read table <fs_v_tab> assigning <wa_tab> WITH KEY (<fs_comp>) = <fs_param>-low.
Max
2009 Dec 02 2:41 PM
You could also try another approach.
Copy your table first,
and then Delete from your table, where your rows don't match.
Grtz
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |