‎2007 Feb 12 3:24 PM
Hi,
Can Anyone give me information on how to create an internal table dynamically and when should we do this?
Also, when do we need an internal table with header line and without header line??
Thanks & Regards,
Ashok.
‎2007 Feb 12 4:26 PM
Hi Ashok Kumar,
Go through t e following sample Code for Dynamic Creation of Internal Tables
=====================================
REPORT zmaschl_create_data_dynamic .
TYPE-POOLS: slis.
DATA: it_fcat TYPE slis_t_fieldcat_alv,
is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data.
DATA: new_line TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
Build fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SYST'
CHANGING
ct_fieldcat = it_fcat[].
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.
MOVE-CORRESPONDING is_fcat TO is_fieldcat.
is_fieldcat-fieldname = is_fcat-fieldname.
is_fieldcat-ref_field = is_fcat-fieldname.
is_fieldcat-ref_table = is_fcat-ref_tabname.
APPEND is_fieldcat TO it_fieldcat.
ENDLOOP.
Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = new_table.
Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.
Test it...
DO 30 TIMES.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
<l_field> = sy-index.
INSERT <l_line> INTO TABLE <l_table>.
ENDDO.
LOOP AT <l_table> ASSIGNING <l_line>.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
WRITE <l_field>.
ENDLOOP.
Regards
Sreeni
‎2007 Feb 12 3:27 PM
‎2007 Feb 12 3:45 PM
Hello Ashok,
Check this.
In the following , finally <fs_4> contains the heading and <fs_1> contains the data
DATA: new_fldcat TYPE lvc_t_fcat.
DATA: new_fldcat_ln TYPE lvc_s_fcat..
DATA: lt_fieldcat_head TYPE lvc_t_fcat,
lt_fcat TYPE lvc_s_fcat,
lt_fcat_h TYPE lvc_s_fcat.
FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
<fs_1> TYPE ANY TABLE,
<fs_2>,
<fs_3>,
<fs>,
<fs_4> TYPE ANY TABLE,
<fs_5>.
DATA: new_line TYPE REF TO data.
DATA: lt_data TYPE REF TO data.
DATA: dyn_lines TYPE i,
DATA : count TYPE i.
CALL METHOD alvgrid_detail->get_backend_fieldcatalog
IMPORTING
et_fieldcatalog = new_fldcat.
*this is for the fieldcatalog whatever u r selecting the fields only
LOOP AT new_fldcat INTO new_fldcat_ln WHERE no_out <> 'X'.
CLEAR: lt_fcat,
lt_fcat_h.
MOVE new_fldcat_ln TO lt_fcat.
APPEND lt_fcat TO lt_fieldcatalog.
lt_fcat_h-fieldname = new_fldcat_ln-fieldname.
lt_fcat_h-inttype = 'C'.
lt_fcat_h-outputlen = STRLEN( new_fldcat_ln-scrtext_l ).
APPEND lt_fcat_h TO lt_fieldcat_head.
ENDLOOP.
**dynamic table creation for data
ASSIGN lt_data TO <fs_data>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcatalog
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc = 0.
ASSIGN <fs_data>->* TO <fs_1>.
CREATE DATA new_line LIKE LINE OF <fs_1>.
A field-symbol to access that work area
ASSIGN new_line->* TO <fs_2>.
***MOVE data
DATA: wa_t_bom LIKE t_bom.
LOOP AT t_bom_pdf INTO wa_t_bom .
MOVE-CORRESPONDING wa_t_bom TO <fs_2>.
INSERT <fs_2> INTO TABLE <fs_1>.
ENDLOOP.
ENDIF.
***CREATE DYNAMIC TABLE FOR HEADER TABLE
CLEAR: <fs_data>,
new_line.
DESCRIBE TABLE lt_fieldcat_head LINES dyn_lines.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcat_head
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc = 0.
ASSIGN <fs_data>->* TO <fs_4>.
CREATE DATA new_line LIKE LINE OF <fs_4>.
A field-symbol to access that work area
ASSIGN new_line->* TO <fs_5>.
**ADD COLUMN HEADER
LOOP AT new_fldcat INTO new_fldcat_ln WHERE no_out <> 'X'.
count = count + 1.
ASSIGN COMPONENT count OF STRUCTURE <fs_5> TO <fs>.
<fs> = new_fldcat_ln-scrtext_l.
<fs> = new_fldcat_ln-seltext.
ENDLOOP.
IF NOT <fs_5> IS INITIAL.
INSERT <fs_5> INTO TABLE <fs_4>.
ENDIF.
ENDIF.
Vasanth
‎2007 Feb 12 4:26 PM
Hi Ashok Kumar,
Go through t e following sample Code for Dynamic Creation of Internal Tables
=====================================
REPORT zmaschl_create_data_dynamic .
TYPE-POOLS: slis.
DATA: it_fcat TYPE slis_t_fieldcat_alv,
is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data.
DATA: new_line TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
Build fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SYST'
CHANGING
ct_fieldcat = it_fcat[].
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.
MOVE-CORRESPONDING is_fcat TO is_fieldcat.
is_fieldcat-fieldname = is_fcat-fieldname.
is_fieldcat-ref_field = is_fcat-fieldname.
is_fieldcat-ref_table = is_fcat-ref_tabname.
APPEND is_fieldcat TO it_fieldcat.
ENDLOOP.
Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = new_table.
Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.
Test it...
DO 30 TIMES.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
<l_field> = sy-index.
INSERT <l_line> INTO TABLE <l_table>.
ENDDO.
LOOP AT <l_table> ASSIGNING <l_line>.
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
WRITE <l_field>.
ENDLOOP.
Regards
Sreeni
‎2007 Feb 12 5:42 PM
Hi,
in Present version, With header line is not the perfect one, it is not good for performance, even SAP itself it is saying that do not use with Header lines, so we need to use the Work Area's
Simple Example For Dynamic Internal table Creation ..
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. Regards
Sudheer