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

Dynamic table created from an Internal table

Former Member
0 Likes
1,540

Hi all,

I searched through the forums and found a few topics on how to create an dynamic table, but the provided solutions were creating dynamic tables from a database table. I was wondering if it would be possible to do the same from an internal table.

Here is the scenario I have an internal table

DATA: BEGIN OF INTERNAL_TABLE OCCURS 10,

LName TYPE P0002-NACHN,

FNAME TYPE P0002-VORNA,

SALARY TYPE P decimals 2,

....

....

END OF INTERNAL_TABLE.

After the table is filled - based on the user's selection (let's say user selected LName and Salary) I need to create a NEW internal table with only two fields LName and Salary .

Thank you

11 REPLIES 11
Read only

Former Member
0 Likes
1,491

Hi Naoki,

Refer this message :

There is a code attached, which you may find useful.

Regards,

Subramanian V.

Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
1,491

Hi Naoki

The thread below is also about your question. The discussions with ABAP descriptor classes may be used.

*--Serdar

Read only

Former Member
0 Likes
1,491

Subramanian and Serdar thank you for your responces.

Subramanian, I've looked through the topic of the link you provided, the method discussed on that topic is exactly what I am having problem with. The problem is they are using TABLES: mara, makt in the provided example.

I don't have ABAP tables, I only have an internal table.

Serdar,

Your sample looks like something I might be able to use, however I don't seem to get what I am trying to get .. when this part is executed

zz_tabletype = cl_abap_tabledescr=>create (.....)

the zz_tabletype looks like this:

Name

-


Field1

Field2

.....

While I need more like this.

Field1 Field2

-


-


Read only

0 Likes
1,491

If you just want to refer to you itab type then you have to manullay build the fieldcat passing the following values.

is_fieldcat-fieldname = <file name u wanted to have>.

is_fieldcat-outputlen = '25' .

is_fieldcat-datatype = <data type char, int, numc etc>.

is_fieldcat-scrtext_l = <text>.

Regards

Raja

Read only

0 Likes
1,491

I am not sure, Naoki, if you saw the code correctly, but the code <b>does not use TABLES</b> as you mention it.

The code creates a <b>Field Catalog</b>, and while creating a field catalog you just need to put in the <b>names of the fields you require</b>. The additional '<b>tabname</b>' field provided is not required, but helps in finding the characteristics of the field like domain name etc..

In case you need an internal table with <b>"your" fields</b>, all you need to do is:

1) Build a fieldcatalog (like its done in ALV) i.e. put all the fieldnames that you require in your internal table and create a fieldcatalog.

2) Call the cl_alv_table_create=>create_dynamic_table method as shown below.

CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = it_fieldcat IMPORTING ep_table = new_table.

The new_table has the fields that you require.

If this does not work...

Either I have not understood your problem correctly or you have not understood my answer correctly.

Regards,

Subramanian V.

Read only

Former Member
0 Likes
1,491

Subramanian,

Thank you for your response, I realized what my problem was, I specified ls_alv_cat-ref_table and ls_alv_cat-ref_field fields and that's what gave me issues even if I omit ls_alv_cat-ref_table. But If I omit both of these fields my issue is solved.

The only problem I am still having is when I don't specify the above-mentioned fields, the data type for all fields gets automatically assigned to 'C'.

Read only

0 Likes
1,491

Hi Naoki,

I am glad , I could be of help to you.

Can you brief, what problem you are facing , if all fields gets automatically assigned to 'C'(character) ?

Any particular conversion problems ?

Not able to add ?

or anything similar to that.

Regards,

Subramanian V.

Read only

Former Member
0 Likes
1,491

Subramanian,

It was essential for me to create a dynamic table that would take the exact attributes (particularly datatypes) of the original table (for selected field).

To do that I used INTTYPE field in the fieldcatalog to pass corresponding datatype (P,C,N,I etc.) It was done in a somewhat brute-force way but it worked.

Thank you.

Read only

0 Likes
1,491

There is no need to hardcode.

You could have used the DESCRIBE FIELD <FIELDNAME> TYPE <FIELDTYPE>.

Regards,

Subramanian V.

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,491

Hi,

here is the 6.40-way to create an dynmaic internal table. You can create dynamic type objects of RTTS (Run Time Type Services). RTTS is well known RTTI enhanced with methods for RTTC (Run Time Type Creation).

<b>DATA: struc_type TYPE REF TO cl_abap_structdescr,

table_type TYPE REF TO cl_abap_tabledescr,

comp_tab TYPE cl_abap_structdescr=>component_table,

comp LIKE LINE OF comp_tab,

sref TYPE REF TO data,

tref TYPE REF TO data.

FIELD-SYMBOLS: <struc> TYPE ANY,

<table> TYPE ANY TABLE.

comp-name = 'column1'.

comp-type = cl_abap_elemdescr=>get_c( 40 ).

APPEND comp TO comp_tab.

comp-name = 'column2'.

comp-type = cl_abap_elemdescr=>get_i( ).

APPEND comp TO comp_tab.

struc_type = cl_abap_structdescr=>create( comp_tab ).

table_type = cl_abap_tabledescr=>create( struc_type ).

CREATE DATA: sref TYPE HANDLE struc_type,

tref TYPE HANDLE table_type.

ASSIGN: sref->* TO <struc>,

tref->* TO <table>.

...

LOOP AT <table> ASSIGNING <struc>.

...

ENDLOOP.</b>

Read only

0 Likes
1,491

Hi All,

I have somehow a related scenario here.

For example I have internal table t_output containing 11 columns, how can I create dynamically a new internal table from t_output?