Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically get structure of a dynamic table

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.

like image 579
Niklas Avatar asked Dec 21 '25 10:12

Niklas


2 Answers

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.

like image 112
icbytes Avatar answered Dec 24 '25 11:12

icbytes


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>.
like image 45
Niklas Avatar answered Dec 24 '25 11:12

Niklas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!