Working With Dynamic Tables Using Field Symbols PDF
Working With Dynamic Tables Using Field Symbols PDF
Welcome Guest
SDN Community BPX Community Business Analytics University Alliances SAP EcoHub
Home Forums Wiki Blogs Articles eLearning Downloads Code Exchange Career Center Events InnoCentive Idea Place
Added by dinesh kumar, last edited by Steffen Frhlich on Feb 11, 2011
Working with dynamic tables using field symbols
There can be multiple materials on dynamic internal table and their working,but their usage in accordance with the field symbols is different and learnable process in its own way.
DYNAMIC TABLE : As the name defines,is a table created at run time.
USE : when a structure of a table is not determined till the point of execution and it is required to output the changed data .
Creation of dynamic table with explanation followed by example :
To create a dynamic table,a structure has to be created,
A method 'CREATE' of class 'CL_ABAP_STRUCTDESCR' has to be called to create the dynamic structure,
New_type = cl_abap_structdescr=>create( new_struc ).
where, new_struc is the table filled as shown in example below,
l_struct ?= cl_abap_typedescr=>describe_by_name( 'TY_OUTTAB' ).
wa_new_struc = l_struct->get_components( ).
APPEND LINES OF wa_new_struc TO new_struc.
where, ty_outtab has the other fields which are needed in addition to fields to be added at run time. Example:
Types: begin of ty_outtab,
matnr type matnr,
makt type makt,
count type char 5,
End of ty_outtab.
Now, to add additional fields to new structure and name them as count1, count2 and so on.
DO v_number TIMES.
l_element ?= cl_abap_elemdescr=>describe_by_name( 'COUNT' ).
CONCATENATE 'ABAP' sy-tabix INTO wa_newstruc-name.
wa_newstruc-type = cl_abap_elemdescr=>get_c(
p_length = lo_element->length ).
APPEND wa_newstruc TO new_struc.
ENDDO.
A structure with 'v_number' number of columns is created where in the fields are created with name ABAP followed by counter(sy-tabix).
Now the table has to be created with this new structure calling the method 'CREATE' of class 'cl_abap_tabledescr' as,
new_tab = cl_abap_tabledescr=>create(
p_line_type = new_struc
p_table_kind = cl_abap_tabledescr=>tablekind_std /* (table created as standard table)
p_unique = abap_false ).
Now the fields in the table can be referenced through the data referenced variables as,
CREATE DATA l_data TYPE HANDLE new_tab.
CREATE DATA l_wa TYPE HANDLE new_struc.
where, l_data is a data referenced variable ie., declared of type DATA.
CREATE DATA creates a data object in the internal session of ABAP program and assigns the reference
to the l_data reference variable.
The content of a data object that is created with CREATE DATA(l_data) can only be accessed using dereferenced data reference variables
or field symbols.
ASSIGN l_wa->* TO . "To address the new structure i.e.,a work area for the newly created table.
ASSIGN l_data->* TO .
where <f_tab> is of any table type.
The table with the required number of fields and corresponding names is created.
To fill the table :
1.Direct selection from data base tables, see this example:
SELECT *
FROM dbtable
INTO <f_tab>.
this is a straight forward method and can be used with much ease.
Page 1 of 2 SAP Community Network Wiki - ABAP Development - Working with dynamic tables...
04-08-2011 http://wiki.sdn.sap.com/wiki/display/ABAP/Working+with+dynamic+tables+using+f...
2) If it needs to be filled based on some conditions without direct fetch from data base,
there the beauty of field symbols come into picture.
For example,
Lets fill the above created dynamic table(<f_tab>) based on values existing in another table(t_outtab)
loop at t_outtab into wa_out.
v_var = wa_out-order.
" CONCATENATE '-COUNT' v_var INTO v_var.
" ASSIGN (v_var) TO .
" = wa_out-matnr.
" v_var = '-MATNR' .
" ASSIGN (v_var) TO .
" = wa_out-matnr.
" v_var = '-MAKT' .
" ASSIGN (v_var) TO .
" = wa_out-makt.
" INSERT INTO TABLE .
" CLEAR: lv_counter,
,
.
endloop.
Explanation :
let's suppose that new table is created with 2 constant and 4 dynamic fields,
matnr,makt,count1, count2,count3,count4.
Now,to fill <f_tab> with <F1>, <f1> has to be filled with the value,
but field is defined as COUNT as defined in ty_outtab structure but not COUNT1 or COUNT2 ..,
Therefore it has to be concatenated with v_var which is 1 say.
Thus v_var is now <F1>-count1 after step1.
we cannot insert a value into <F1>-count1 directly ,so,another field symbol <FS> of type ANY is used.
now,<FS> is assigned the value present in '<F1>-count1' which is blank after step 2.
In step 3, <fs> is filled with a value which overwrites the already present value(which is blank in step2 ).
like wise, matnr and makt are filled.
Thus,<F1> is filled like,
<F1>-matnr = 90000.
<F1>-makt = chocolate.
<F1>-count1 = order1.
<F1>-count2 = order2.
<F1>-count3 = order3.
<F1>-count4 = order4.
Then at step 5 this dynamic structure<F1> is inserted into dynamic table <f_tab>.
This is how a table is created and filled using field symbols at run time.
Comments (1)
Contact Us Site Index Marketing Opportunities Legal Terms Privacy Impressum
Powered by SAP NetWeaver
Page 2 of 2 SAP Community Network Wiki - ABAP Development - Working with dynamic tables...
04-08-2011 http://wiki.sdn.sap.com/wiki/display/ABAP/Working+with+dynamic+tables+using+f...