Abap Dynamic Table
Abap Dynamic Table
11, 2011 (view change) show comment 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 proc ess 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 a nd 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 'CREA TE' 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 cr eated as standard table) p_unique = abap_false ). Now the fields in the table can be referenced through the data referenced variab les 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 as signs the reference to the l_data reference variable. The content of a data object that is created with CREATE DATA(l_data) can only b e accessed using dereferenced data reference variables or field symbols. ASSIGN l_wa->* TO . r the newly created table. ASSIGN l_data->* TO . "To address the new structure i.e.,a work area fo
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. 2) If it needs to be filled based on some conditions without direct fetch from d ata 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 a nother 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.
" 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 o r 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 ste p 2. In step 3, <fs> is filled with a value which overwrites the already present valu e(which is blank in step2 ). like wise, matnr and makt are filled. Thus,<F1> is filled like, <F1>-matnr <F1>-makt <F1>-count1 <F1>-count2 <F1>-count3 <F1>-count4 = = = = = = 90000. chocolate. order1. order2. order3. 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.
---------------------------------------------------------------------------------------------------------------------------------x ---------------------------------------------------------------------------------------------------------------------------------x Dynamic Internal table Added by Richa Khosla, last edited by Sandra Rossi on Nov 03, 2009 (view ch ange)
show comment Dynamic internal Table A Dynamic Internal Table is an internal table with variab le number of rows and columns, which can be defined during run time only. A dynamic internal table is not declared in the program as static. Some of the benefits of Dynamic internal table are: Flexiblilty Extremely useful when the numbers of columns / fields are not known at the d esign time / compile time. Avoids redundancy
Some of the important attributes of an internal table which can be set dynamical ly are Column position Field name of internal table field Column width Reference field Reference table Data type of the fields Domain name of the fields Check table etc. (For the remaining attributes please refer the ABAP structure LVC_S_FCAT) rerequisites of dynamic internal table are Knowledge of field symbols Knowledge on data references Field symbols are placeholders or symbolic names for other fields. They are simi lar to dereference pointers in C. Field symbols allow you to: Assign an alias to a data object Adopt or change the type and size of a data object dynamically at runtime Set the offset and length for a string variably at runtime Set a pointer to a data object that you determine at runtime (dynamic ASSIGN ) Access components of a structure The statement ASSIGN f to <fs> assigns the field f to field symbol <fs>. The field symbol <fs> then "points" to the contents of field f at runtime. This mea ns that all changes to the contents of f are visible in <fs> and vice versa. You declare the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>. P
Data references are pointers to data objects. You can use data references to create data objects dynamically. You can also create references to existing data objects. You can only dereference a data reference using a special assignment to a fie ld symbol.
You can create a data reference variable by using: DATA <dref> TYPE REF TO DATA.
To create a data object dynamically during a program, you need a data referen ce variable and the following statement: CREATE DATA <dref> TYPE <type>|LIKE <obj>.
To access the contents of the data object to which a data reference is pointin g, you must dereference it. ASSIGN <dref>->* TO <FS>. Dynamic internal tables can be created u sing : The class CL_ALV_TABLE_CREATE and the method CREATE_DYNAMIC_TABLE. Export parameter: it_fieldcatalog Import parameter: ep_table Exceptions: generate_subpool_dir_full = 1 Others = 2 Structure for Dynamic Internal Table Cre ation ABAP Structure LVC_S_FCAT This structure is used to maintain the attributes of each field of the Dynamic Internal Table such as the fieldname, column position, etc.
ABAP Table type LVC_T_FCAT This table type has a line type of LVC_S_FCAT. The field attribute s of all the Fields are maintained in this table Steps to create a Dynamic Inte rnal Table
Data Definitions Declare a structure of type lvc_s_fcat. Declare an internal table of type lvc_t_fcat (The line type of this internal tab le is lvc_s_fcat). Declare two data reference variables, one for the dynamic internal table (say dr 1) and the other for the work area (say dr2) Declare field symbols of type 'ref to data', 'any table' and of type 'any' (say fs1, fs2 and fs3 respectively). # Populate the internal table with fieldnames r equired for the dynamic internal table a).Assign the field name, field type, field width, check table etc. to the struc ture. b).Append the structure to the internal table # Assign Field-Symbol to dynamic internal table i.e. Assign dr1 to <fs1> # Call the method CREATE_DYNAM IC_TABLE a).Pass the internal table containing the field attributes as the export paramet er. b).Pass the field symbol of the dynamic internal table as the import parameter. # This creates the dynamic internal table <fs1> now refers to dynamic internal table that we wanted to create at start. # Assign the data contents of <fs1> to a field-symbol <fs2> (deref erencing). So <fs2> now points to the dynamic internal table. # Next step is t o create a work area for our dynamic internal table. Create data dr2 like line of <fs2>. # A field-symbol to access that work area Assign dr2->* to <FS_2>. Drawbacks of a Dynamic Internal Table Programs with many dynamic internal tables are less readable and they are le ss secure, since error cannot be detected by syntax check, but only by the runtime system. Performance when a Dynamic Internal Table is used will not be as good as, wh en a Static internal table is used. Sample Program example: how to create a dynamic internal table 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 dynamic internal table********* PERFORM get_data. PERFORM write_out. *&---------------------------------------------------------------------* *& Form get_structure *&---------------------------------------------------------------------* 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 . * Correction by Paul Robert Oct 28, 2009 17:04 * xfc-datatype = xdetails-type_kind. CASE xdetails-type_kind. WHEN 'C'. xfc-datatype = 'CHAR'. WHEN 'N'. xfc-datatype = 'NUMC'. WHEN 'D'. xfc-datatype = 'DATE'. WHEN 'P'. xfc-datatype = 'PACK'. WHEN OTHERS. xfc-datatype = xdetails-type_kind. ENDCASE. xfc-inttype = xdetails-type_kind. xfc-intlen = xdetails-length. xfc-decimals = xdetails-decimals. APPEND xfc TO ifc. ENDLOOP. ENDFORM. "get_structure *&---------------------------------------------------------------------* *& Form create_dynamic_itab *&---------------------------------------------------------------------* 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 i_length_in_byte = 'X' "added by Paul Robert Oct 28, 2009 17:04 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. "create_dynamic_itab *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* FORM get_data. * Select Data from table. SELECT * INTO TABLE <dyn_table> FROM (p_table). ENDFORM. "get_data *&---------------------------------------------------------------------* *& Form write_out *&---------------------------------------------------------------------* FORM write_out. 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. ENDFORM. "write_out