‎2007 Oct 26 1:54 PM
Hi All,
Can anybody provide me with eample of creating Dynamic internal tables?
Regards,
Parvez.
‎2007 Oct 26 4:20 PM
Hi Parvez,
This is the latest way of creating and using dynamic internal tables.
we have 2 concepts called : RTTI and RTTC. Both together called as RTTS.
Just copy the below code into se38 and execute. Give any table name it will create a dynamic table and show the contents in ALV.
REPORT zgs_rttc.
PARAMETERS p_par TYPE dd02l-tabname.
DATA : r_descr TYPE REF TO cl_abap_structdescr,
r_tab TYPE REF TO cl_abap_tabledescr.
DATA : itab TYPE REF TO data.
FIELD-SYMBOLS : <fs> TYPE STANDARD TABLE.
START-OF-SELECTION.
r_descr ?= cl_abap_structdescr=>describe_by_name( p_par ).
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = r_descr
RECEIVING
p_result = r_tab .
CREATE DATA itab TYPE HANDLE r_tab.
ASSIGN itab->* TO <fs>.
SELECT *
FROM (p_par)
INTO TABLE <fs>
UP TO 10 ROWS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = p_par
TABLES
t_outtab = <fs>.
If you want to know more about these concepts do get back to me.
Reward points if useful,
Aleem.
‎2007 Oct 26 2:02 PM
Here is the sample code...
REPORT ZREPORT_TEST.
TYPE-POOLS SLIS.
DATA:
BEGIN OF T_MARC OCCURS 0,
MATNR LIKE MARC-MATNR,
WERKS LIKE MARC-WERKS,
END OF T_MARC.
SELECT MATNR WERKS
FROM MARC
INTO TABLE T_MARC UP TO 10 ROWS.
DATA:
T_FIELDCAT TYPE LVC_T_FCAT WITH HEADER LINE,
T_FCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
data: NEW_table type ref to data.
data: new_line type ref to data.
FIELD-SYMBOLS: <l_table> TYPE table,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
T_FIELDCAT-FIELDNAME = 'MATNR'.
T_FIELDCAT-REF_TABLE = 'MARC'.
APPEND T_FIELDCAT.
T_FIELDCAT-FIELDNAME = 'WERKS'.
T_FIELDCAT-REF_TABLE = 'MARC'.
APPEND T_FIELDCAT.
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = T_FIELDCAT[]
importing
ep_table = NEW_table .
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <l_line> TO <l_field>.
<L_FIELD> = '12345'.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <l_line> TO <l_field>.
<L_FIELD> = '1000'.
APPEND <L_LINE> TO <L_TABLE>.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <l_line> TO <l_field>.
<L_FIELD> = '52349'.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <l_line> TO <l_field>.
<L_FIELD> = '2000'.
APPEND <L_LINE> TO <L_TABLE>.
*LOOP AT <L_TABLE> INTO <L_LINE>.
*WRITE: / <L_LINE>.
*ENDLOOP.
LOOP AT T_FIELDCAT.
T_FCAT-FIELDNAME = T_FIELDCAT-FIELDNAME.
T_FCAT-REF_TABNAME = T_FIELDCAT-REF_TABLE.
APPEND T_FCAT.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = T_FCAT[]
TABLES
t_outtab = <l_table>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
‎2007 Oct 26 2:03 PM
report zrich_0002.
type-pools: slis.
field-symbols: <dyn_table> type standard table,
<dyn_wa>,
<dyn_field>.
data: alv_fldcat type slis_t_fieldcat_alv,
it_fldcat type lvc_t_fcat.
type-pools : abap.
data : it_details type abap_compdescr_tab,
wa_details type abap_compdescr.
data : ref_descr type ref to cl_abap_structdescr.
data: new_table type ref to data,
new_line type ref to data,
wa_it_fldcat type lvc_s_fcat.
selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.
* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
loop at it_details into wa_details.
clear wa_it_fldcat.
wa_it_fldcat-fieldname = wa_details-name .
wa_it_fldcat-datatype = wa_details-type_kind.
wa_it_fldcat-inttype = wa_details-type_kind.
wa_it_fldcat-intlen = wa_details-length.
wa_it_fldcat-decimals = wa_details-decimals.
append wa_it_fldcat to it_fldcat .
endloop.
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.
assign new_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
* Select Data from table.
select * into corresponding fields of table <dyn_table>
from (p_table).
* 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,
Rich Heilman
‎2007 Oct 26 4:20 PM
Hi Parvez,
This is the latest way of creating and using dynamic internal tables.
we have 2 concepts called : RTTI and RTTC. Both together called as RTTS.
Just copy the below code into se38 and execute. Give any table name it will create a dynamic table and show the contents in ALV.
REPORT zgs_rttc.
PARAMETERS p_par TYPE dd02l-tabname.
DATA : r_descr TYPE REF TO cl_abap_structdescr,
r_tab TYPE REF TO cl_abap_tabledescr.
DATA : itab TYPE REF TO data.
FIELD-SYMBOLS : <fs> TYPE STANDARD TABLE.
START-OF-SELECTION.
r_descr ?= cl_abap_structdescr=>describe_by_name( p_par ).
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = r_descr
RECEIVING
p_result = r_tab .
CREATE DATA itab TYPE HANDLE r_tab.
ASSIGN itab->* TO <fs>.
SELECT *
FROM (p_par)
INTO TABLE <fs>
UP TO 10 ROWS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = p_par
TABLES
t_outtab = <fs>.
If you want to know more about these concepts do get back to me.
Reward points if useful,
Aleem.