Dynamic Internal Table
Dynamic Internal Table
Dynamic Internal Table
Dynamic internal Table A Dynamic Internal Table is an internal table with variable 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 design time / compile time. Avoids redundancy
Some of the important attributes of an internal table which can be set dynamically 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) Prerequisites 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 similar 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 means 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>.
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 field 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 reference 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 pointing, you must dereference it. ASSIGN <dref>->* TO <FS>. Dynamic internal tables can be created using :
Export parameter: it_fieldcatalog Import parameter: ep_table Exceptions: generate_subpool_dir_full = 1 Others = 2 Structure for Dynamic Internal Table Creation
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 attributes of all the Fields are maintained in this table Steps to create a Dynamic Internal Table
1. 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 table is lvc_s_fcat). Declare two data reference variables, one for the dynamic internal table (say dr1) 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 required for the dynamic internal table a).Assign the field name, field type, field width, check table etc. to the structure. 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_DYNAMIC_TABLE a).Pass the internal table containing the field attributes as the export parameter.
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> (dereferencing). So <fs2> now points to the dynamic internal table. # Next step is to 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 less 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, when a Static internal table is used.
* 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