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 internal table creation

Former Member
0 Likes
698

Hi All,

I have a problem with creation of an internal table.

First intarnal table contain two fields name and value.

Second table will be created according to this name value pair.

loop at itab1

itab2-??? = itab1-value.

append itab2.

endloop.

How Should I do dynamically so that itab2 can be created.

Regards,

Jeetu

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
680

Hi,

This is very good example ,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

6 REPLIES 6
Read only

Former Member
0 Likes
680

Hi All,

Can I use field symbols in this case. Could anyboby propose any solution?

Read only

Former Member
0 Likes
681

Hi,

This is very good example ,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

Read only

0 Likes
680

Hi Murli,

How can I get the internal table from ur example.

Read only

0 Likes
680

Hi,

FIELD-SYMBOLS: <gt_table> TYPE table.

In field catelog we are appending the column names and length .

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

add one more field to field to field catelog .

Here we are passing field catelog to method to create internal table.

Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

what ever importing structure we are getting gp_table

that we are assignig to field sysmbol gt_table.

You know that field symbol can take any structure dynamically.

here we are assigning workarea to internal table.

DATA: WA TYPE REF TO DATA.

Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

Read only

0 Likes
680

Hi All,

In my case the scnario is a different one.

Let me explain it again. I have table dttab has many fields.

Fields of dttab : f1,f2,f3,f4 etc.

Now I will get one internal table as itab1 with two fields :name and value. The name field contains the name of fields of dttab. i.e f1, aaa

f2, bbb

f3, ccc

Now I need to create an internal table itab2 of type dttab but dynamically.

loop at itab1.

itab2-??? = itab1-value ( like 'aaa', 'bbb' etc)

append itab2.

endloop.

In place of ??? I need to send f1,f2,f3 etc. But how can I detemine it dynamically.

Regards,

Jeetu

Read only

former_member188594
Active Participant
0 Likes
680

Hi Jeetu,

Please refer the following link which explains the concept of dynamic internal table very clearly.

[http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm]

Reward points if useful.

Regards,

sekhar.