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 with the same name

Former Member
0 Likes
1,534

Hey Guys

I have a question.

I know that we can create dynamic internal table taking a struct dynamically.

But I have 2 ques on the same subject.

1. Can we create an internal table based on a type that we have locally declared eg

types: begin of ty_demo.

var1(1) type c,

var2 type p,

end of ty_demo.

2. If an internal table is already declared based on the above type say data: i_tab type standard table of ty_demo.

If i need to enhance the struct of this internal table. Can i do that by dynamically redefining it based on a different structure but keepin the same name i_tab.

In a nut shell I cannot change the name of my itab but I want to enhance the structure.

I Hope I am clear.

Help will be greatly apprcieated.

Thanks

Sameer

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
866

Hi,

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

with regards,

sowjanya

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
866

1. With CREATE DATA ... TYPE ... , you can reference a local type:

data ref type ref to data.
create data ref type ty_demo.

Then you can use ref by

field-symbols <stru> type any.
assign ref->* to <stru>

etc. (see sap doc for more info)

2. To create anything dynamically, refer to RTTC classes. Demo program:

REPORT  ZDYNSTRUCTCREATE.

PERFORM MAIN.

FORM MAIN.

type-pools abap.
data:
ls_component type abap_componentdescr,
lt_component type abap_component_tab.

*... (1) define structure components :

clear ls_component.
ls_component-name = 'CARRID'.
ls_component-type ?= cl_abap_typedescr=>describe_by_name( 'SFLIGHT-CARRID' ).
insert ls_component into table lt_component.

clear ls_component.
ls_component-name = 'CONNID'.
ls_component-type ?= cl_abap_elemdescr=>get_n( 4 ).
insert ls_component into table lt_component.

*... (2) create structure
data lr_strucdescr type ref to cl_abap_structdescr.
data lr_data_struc type ref to data.

lr_strucdescr = cl_abap_structdescr=>create( lt_component ).
create data lr_data_struc type handle lr_strucdescr.

*... (2b) fill structure
field-symbols: <ls_struc> type any.
assign lr_data_struc->* to <ls_struc>.

field-symbols: <l_field> type any.
data l_fieldname type fieldname.

l_fieldname = 'CARRID'.
assign component l_fieldname of structure <ls_struc> to <l_field>.
if sy-subrc = 0.
<l_field> = 'AF'.
endif.

l_fieldname = 'CONNID'.
assign component l_fieldname of structure <ls_struc> to <l_field>.
if sy-subrc = 0.
<l_field> = '3878'.
endif.

*... (3) create table
data lr_tabledescr type ref to cl_abap_tabledescr.
data lr_data_table type ref to data.

lr_tabledescr = cl_abap_tabledescr=>create( p_line_type = lr_strucdescr ).

create data lr_data_table type handle lr_tabledescr.

*... (3b) fill table
field-symbols:
<lt_table> type standard table.

assign lr_data_table->* to <lt_table>.

append <ls_struc> to <lt_table>.

ENDFORM.

Read only

Former Member
0 Likes
867

Hi,

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

with regards,

sowjanya

Read only

Former Member
0 Likes
866

hai.

we can create an internal table based on a type that we have locally declared, but you have to use data instead of types like.

data: begin of ty_demo.

var1(1) type c,

var2 type p,

end of ty_demo.

check the example notes for dynamic itab .

Dynamic internal table is internal table that we create on the fly with flexible column numbers.

For sample code, please look at this code tutorial. Hopefully it can help you

Check this link:

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

Sample code:

DATA: l_cnt(2) TYPE n,

l_cnt1(3) TYPE n,

l_nam(12),

l_con(18) TYPE c,

l_con1(18) TYPE c,

lf_mat TYPE matnr.

SORT it_bom_expl BY bom_comp bom_mat level.

CLEAR: l_cnt1, <fs_dyn_wa>.

  • Looping the component internal table

LOOP AT it_bom_expl INTO gf_it_bom_expl.

CLEAR: l_cnt1.

AT NEW bom_comp.

CLEAR: l_cnt, <fs_dyn_wa>, lf_mat.

  • For every new bom component the material data is moved to

  • temp material table which will be used for assigning the levels

  • checking the count

it_mat_temp[] = it_mat[].

  • Component data is been assigned to the field symbol which is checked

  • against the field of dynamic internal table and the value of the

  • component number is been passed to the dynamic internal table field

  • value.

ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa> TO

<fs_check>.

<fs_check> = gf_it_bom_expl-bom_comp.

ENDAT.

AT NEW bom_mat.

CLEAR l_con.

ENDAT.

lf_mat = gf_it_bom_expl-bom_mat.

  • Looping the temp internal table and looping the dynamic internal table

*by reading line by line into workarea, the materialxxn is been assigned

  • to field symbol which will be checked and used.

LOOP AT it_mat_temp.

l_nam = c_mat.

l_cnt1 = l_cnt1 + 1.

CONCATENATE l_nam l_cnt1 INTO l_nam.

LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.

ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.

ENDLOOP.

IF <fs_xy> = lf_mat.

CLEAR lf_mat.

l_con1 = l_con.

ENDIF.

  • Checking whether the material exists for a component and if so it is

  • been assigned to the field symbol which is checked against the field

  • of dynamic internal table and the level of the component number

  • against material is been passed to the dynamic internal table field

  • value.

IF <fs_xy> = gf_it_bom_expl-bom_mat.

ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_check>.

CLEAR l_con.

MOVE gf_it_bom_expl-level TO l_con.

CONCATENATE c_val_l l_con INTO l_con.

CONDENSE l_con NO-GAPS.

IF l_con1 NE space.

CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.

CLEAR l_con1.

l_cnt = l_cnt - 1.

ENDIF.

<fs_check> = l_con.

l_cnt = l_cnt + 1.

ENDIF.

ENDLOOP.

AT END OF bom_comp.

  • At end of every new bom component the count is moved to the field

  • symbol which is checked against the field of dynamic internal table

  • and the count is been passed to the dynamic internal table field

  • value.

ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa> TO <fs_check>.

<fs_check> = l_cnt.

INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.

ENDAT.

ENDLOOP.

regards.

sowjanya.b

Read only

Former Member
0 Likes
866

Thanx guys