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

Dynamicly creating an internal table with header line

Olivier_S
Participant
0 Likes
1,904

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!

1 ACCEPTED SOLUTION
Read only

rainer_hbenthal
Active Contributor
0 Likes
1,844

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 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!

11 REPLIES 11
Read only

rainer_hbenthal
Active Contributor
0 Likes
1,845

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.

Read only

0 Likes
1,844

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

Read only

0 Likes
1,844

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

Read only

0 Likes
1,844

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.

Read only

0 Likes
1,844

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

Read only

0 Likes
1,844

Looping with field symbols as shown by some examples isnt so horrible/bad performant.

Read only

0 Likes
1,844

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,844

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

Read only

Clemenss
Active Contributor
0 Likes
1,844

This message was moderated.

Read only

Former Member
0 Likes
1,844

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

Read only

Former Member
0 Likes
1,844

You could also try another approach.

Copy your table first,

and then Delete from your table, where your rows don't match.

Grtz