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

reg:doubt in dynamic generated internal table

Former Member
0 Likes
531

hi everyone,

i generated a internal table dynamically,the output is in fied symbol,i want to create a internal table using the field symbol sturture,how to create that????

plz tell me how to create a internal table referencing field symbol structure........

very urgent plz help.........

thanks in advance....

regards,

balaji.s

4 REPLIES 4
Read only

Former Member
0 Likes
506

Here is a sample code to create an internal table dynamically.

TYPE-POOLS:

abap.

DATA:

gr_structdescr TYPE REF TO cl_abap_structdescr,

gr_tabledescr TYPE REF TO cl_abap_tabledescr,

gt_components TYPE abap_component_tab,

gw_component TYPE LINE OF abap_component_tab,

gt_keys TYPE abap_keydescr_tab,

gw_key TYPE LINE OF abap_keydescr_tab,

gr_wa TYPE REF TO data,

gr_itab TYPE REF TO data.

FIELD-SYMBOLS:

<gw_wa> TYPE ANY,

<gt_itab> TYPE ANY TABLE.

START-OF-SELECTION.

  • determine components of structure -> GT_COMPONENTS

MOVE 'COMP1' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'COMP2' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_i( ).

INSERT gw_component INTO TABLE gt_components.

  • get structure descriptor -> GR_STRUCTDESCR

gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).

  • create work area of structure GR_STRUCTDESCR -> GR_WA

CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

ASSIGN gr_wa->* TO <gw_wa>.

  • determine key components -> GT_KEYS

MOVE 'COMP1' TO gw_key-name.

INSERT gw_key INTO TABLE gt_keys.

  • create descriptor for internal table -> GR_TABLEDESCR

gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = gr_structdescr

p_table_kind = cl_abap_tabledescr=>tablekind_hashed

p_unique = abap_true

p_key = gt_keys

p_key_kind = cl_abap_tabledescr=>keydefkind_user ).

  • create internal table -> GR_ITAB

CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.

ASSIGN gr_itab->* TO <gt_itab>.

sy-subrc = sy-subrc.

Reward points

Read only

Former Member
0 Likes
506

hai.

check this.

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
506

Hi ,

check out a small part of an alv report which is using dynamic internal table

data: dy_table type ref to data,

dy_line type ref to data.

field-symbols: <dyn_table> type standard table,

<dyn_wa> type any,

<dyn_line> type any.

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 A DYNAMIC WORK AREA.

create data dy_line like line of <dyn_table>.

sort it_mard by matnr werks.

loop at it_mard into wa_mard.

at new matnr.

assign dy_line->* to <dyn_wa>.

assign component 'MATNR' of structure <dyn_wa> to <dyn_line>.

<dyn_line> = wa_mard-matnr.

read table it_mard2 into wa_mard1 with key matnr = wa_mard-matnr.

v_amount = wa_mard1-v_total * p_unit.

call function 'SPELL_AMOUNT'

exporting

amount = v_amount

  • currency = 'INR'

  • FILLER = ' '

language = sy-langu

importing

in_words = v_price

exceptions

not_found = 1

too_large = 2

others = 3

.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

assign component 'V_TOTAL' of structure <dyn_wa> to <dyn_line>.

<dyn_line> = wa_mard1-v_total.

assign component 'V_PRICE' of structure <dyn_wa> to <dyn_line>.

<dyn_line> = v_price-word.

endat.

concatenate 'STOCK' '-' 'IN' '-' wa_mard-werks into v_new.

assign component v_new of structure <dyn_wa> to <dyn_line>.

<dyn_line> = wa_mard-labst.

at end of matnr.

append <dyn_wa> to <dyn_table>.

clear wa_mard.

clear <dyn_wa>.

unassign <dyn_line>.

unassign <dyn_wa>.

endat.

endloop.

rewards points if helpful.

Read only

Former Member
0 Likes
506

Hi,

Please check this code may be useful.

TYPE-POOLS:

abap.

DATA:

gr_structdescr TYPE REF TO cl_abap_structdescr,

gr_tabledescr TYPE REF TO cl_abap_tabledescr,

gt_components TYPE abap_component_tab,

gw_component TYPE LINE OF abap_component_tab,

gt_keys TYPE abap_keydescr_tab,

gw_key TYPE LINE OF abap_keydescr_tab,

gr_wa TYPE REF TO data,

gr_itab TYPE REF TO data.

FIELD-SYMBOLS:

<gw_wa> TYPE ANY,

<gt_itab> TYPE ANY TABLE.

START-OF-SELECTION.

determine components of structure -> GT_COMPONENTS

MOVE 'COMP1' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'COMP2' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_i( ).

INSERT gw_component INTO TABLE gt_components.

get structure descriptor -> GR_STRUCTDESCR

gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).

create work area of structure GR_STRUCTDESCR -> GR_WA

CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

ASSIGN gr_wa->* TO <gw_wa>.

determine key components -> GT_KEYS

MOVE 'COMP1' TO gw_key-name.

INSERT gw_key INTO TABLE gt_keys.

create descriptor for internal table -> GR_TABLEDESCR

gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = gr_structdescr

p_table_kind = cl_abap_tabledescr=>tablekind_hashed

p_unique = abap_true

p_key = gt_keys

p_key_kind = cl_abap_tabledescr=>keydefkind_user ).

create internal table -> GR_ITAB

CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.

ASSIGN gr_itab->* TO <gt_itab>.

sy-subrc = sy-subrc.

Regards,

Muneesh Gitta.