1

In a method I have a reference to a table that was declared like this:

DATA: tabname TYPE tabname,
      dref    TYPE REF TO data,
FIELD-SYMBOLS: <itab> TYPE ANY TABLE.

CREATE DATA dref TYPE TABLE OF (tabname).
ASSIGN dref->* TO <itab>.

SELECT * FROM (tabname)
    UP TO 5 ROWS
  INTO TABLE <itab>.

How do I create a structure based on ?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
kdobrev
  • 270
  • 1
  • 3
  • 11

3 Answers3

3

Just use good ol' RTTS for that. You can create reference and read directly into it

FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE.
DATA: ref_wa        TYPE REF TO data,
      ref_rowtype   TYPE REF TO cl_abap_structdescr,
      ref_tabletype TYPE REF TO cl_abap_tabledescr.

ref_rowtype ?= cl_abap_typedescr=>describe_by_name( tabname ).

CREATE DATA ref_wa TYPE HANDLE ref_rowtype.
READ TABLE <itab> REFERENCE INTO ref_wa INDEX 1.

or create field-symbol based on this reference and use it in READ TABLE

ASSIGN ref_wa->*  TO FIELD-SYMBOL(<fsym_wa>).
READ TABLE <itab> ASSIGNING <fsym_wa> INDEX 1.

Pay attention I declared <itab> as STANDARD table to get rid of index error operation you got.

enter image description here

UPDATE: for creating structure from <itab> object use this syntax:

ref_tabletype ?= cl_abap_typedescr=>describe_by_data( <itab> ).
ref_rowtype  ?= ref_tabletype->get_table_line_type( ).

The last two lines will be identical.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • Thank you for your pointing. But just trying to master RTTS - can I use this solution with , not with a name of a dict object? – kdobrev Sep 03 '18 at 12:45
1

1.You define a ANY field symbol ans use ASSIGNING

 FIELD-SYMBOLS:
      <line> type any.

 LOOP at <itab> ASSIGNING <line>.

 ENDLOOP.

2.You define a ANY field symbol ans use INTO

 FIELD-SYMBOLS:
      <line> type any.

 CREATE DATA dref like line of <itab>.
 ASSIGN dref->* to <line>.

 LOOP at <itab> INTO <line>.

 ENDLOOP.
Haojie
  • 5,665
  • 1
  • 15
  • 14
  • Agreed but I don't see the interest of the solution 2 with CREATE DATA and INTO (more complex, less performant, no advantage) – Sandra Rossi Sep 03 '18 at 19:47
  • @SandraRossi Solution 2 shows how to dynamically define a type of a line of table. It is just one of the answer to the question. Both INTO and ASSIGNING has its own pitfalls. – Haojie Sep 04 '18 at 01:05
0

You can use inline declaration to define WA like this. READ TABLE <itab> INTO (<wa>) or declare WA/Field symbol first using FIELD SYMBOL <wa> TYPE ANY then read table with READ TABLE <tab> ASSIGNING <wa>

Binh
  • 241
  • 1
  • 15
  • READ TABLE ASSIGNING INDEX row. gives me: You cannot use explicit or implicit index operations on tables with types "HASHED TABLE" or "ANY TABLE". "" has the type "ANY TABLE". It is possible that the "TABLE" addition was not specified before "". – kdobrev Sep 03 '18 at 06:28
  • READ TABLE INTO () INDEX row. gives me: Field "()" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement. . – kdobrev Sep 03 '18 at 06:32
  • Maybe a typo. Please try `FIELD-SYMBOLS . READ TABLE INTO WITH KEY (fld_name) = "value".` This is exactly the code in my program
    – Binh Sep 03 '18 at 06:41
  • FIELD-SYMBOLS . gives error: Untyped field symbols are not supported in the OO context. Use "FIELD-SYMBOLS ... TYPE ANY". . – kdobrev Sep 03 '18 at 06:44
  • Please take a look at this article https://blogs.sap.com/2014/04/03/create-dynamic-table-using-rtts-and-display-in-alv/ – Binh Sep 03 '18 at 06:47