Question

I want to dynamically get the structure of a dynamic table. Getting the table is no problem, but I stuck with getting the structure of the table.

DATA: lo_dynamic_table TYPE REF TO data.
FIELD-SYMBOLS: <lt_table_structure> TYPE table,
                <ls_table_structure> TYPE any.

CREATE DATA lo_dynamic_table TYPE TABLE OF (lv_my_string_of_table).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.

// some code assigning the structure

Now I want to execute this command:

SELECT SINGLE * FROM (lv_my_string_of_table) INTO <ls_table_structure> WHERE (lv_dynamid_where_field).

If there is any other solution, I will be okay with that.

Was it helpful?

Solution 2

This code worked for my case:

DATA:   table_name type string,
        lo_dynamic_table TYPE REF TO data,
        lo_dynamic_line TYPE REF TO data.

FIELD-SYMBOLS:  <lt_table_structure> TYPE table,
                <ls_table_structure> TYPE any.

table_name = 'yourtablename'.

CREATE DATA lo_dynamic_table TYPE TABLE OF (table_name).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.

CREATE DATA lo_dynamic_line LIKE LINE OF <lt_table_structure>.
ASSIGN lo_dynamic_line->* TO <ls_table_structure>.

OTHER TIPS

Take use of RTTS.

Runtime type services

With this framework You are able to get the desired type during runtime.

http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965

The desired class should be CL_ABAP_TABLEDESCR or even CL_ABAP_DATADESCR.

They will do the work for You.

As it seems like, You are getting a ddic table name and want to select dynamically data from the tablename into a generic internal table.

So, if You are getting already an nice ddic name, then the usage of rtts is even more simple. Because You have the ddic name.

Usually there are also many function modules ( mostly in the namespace-prefix "RPY_*" ). There You surely can find one, which determines the structure of a table, whether it contains includes, and so on. But, try typedescriptors first, I would start with cl_abap_tabledescr=>get_table_line_type.

I added code for dynamic hashed table with dynamic keys.

DATA TAB_NAME LIKE SY-TNAME VALUE 'SCARR'.
DATA KEYTAB TYPE TABLE OF STRING.

DATA DREF TYPE REF TO DATA.
FIELD-SYMBOLS <F_TAB> TYPE ANY TABLE.

APPEND 'CARRID' TO KEYTAB.

CREATE DATA dref TYPE HASHED TABLE OF (TAB_NAME)
                 WITH UNIQUE KEY (KEYTAB).

ASSIGN dref->* TO <F_TAB>.
SELECT *
       FROM (TAB_NAME)
       INTO TABLE <F_TAB>.

cl_demo_output=>display( <F_TAB> ).

Consider also links.

https://help.sap.com/doc/saphelp_nw70/7.0.31/en-US/79/c55497b3dc11d5993800508b6b8b11/content.htm?no_cache=true

https://archive.sap.com/discussions/thread/92739

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top