2007 Feb 24 12:43 PM
HI,
1.CAN WE CREATE INTERNAL TABLE DYNAMICALLY(RUN TIME)?
2007 Feb 24 12:47 PM
<b>Yes , check the program below</b>
&----
*& Report ZUS_SDN_DYNAMIC_ITAB_CREATE
*&
&----
*&
*&
&----
REPORT zus_sdn_dynamic_itab_create.
DATA:
gs_fcat TYPE lvc_s_fcat,
gt_fcat TYPE lvc_t_fcat,
gdo_data TYPE REF TO data.
FIELD-SYMBOLS:
<gt_itab> TYPE table.
START-OF-SELECTION.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'MARA'
CHANGING
ct_fieldcat = gt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL FUNCTION
'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'KNB1'
CHANGING
ct_fieldcat = gt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
DELETE gt_fcat WHERE NOT ( fieldname = 'MATNR' OR
fieldname = 'KUNNR' OR
fieldname =
'BUKRS' ).
LOOP AT gt_fcat INTO gs_fcat.
gs_fcat-col_pos = syst-tabix.
MODIFY gt_fcat FROM gs_fcat INDEX syst-tabix.
ENDLOOP.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
I_STYLE_TABLE =
it_fieldcatalog = gt_fcat
I_LENGTH_IN_BYTE =
IMPORTING
ep_table = gdo_data
E_STYLE_FNAME =
EXCEPTIONS
generate_subpool_dir_full = 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.
ASSIGN gdo_data->* TO <gt_itab>.
APPEND INITIAL LINE TO <gt_itab>.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
it_fieldcat_lvc = gt_fcat
TABLES
t_outtab = <gt_itab>
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.
END-OF-SELECTION.
2007 Feb 24 2:57 PM
Hi,
I did this in one of my programs.C if it of any help to u.
REPORT zvb_dynamic_tab.
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
&----
text
----
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[].
data: idetails type table of TVM1t,
xdetails type tvm1t.
select * from TVM1T into table idetails.
xfc-fieldname = 'VBELN'.
xfc-datatype = 'VBELN_VA'. "xdetails-type_kind.
APPEND xfc TO ifc.
LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-BEZEI.
xfc-datatype = 'NETPR'. "xdetails-type_kind.
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length.
xfc-decimals = xdetails-decimals.
APPEND xfc TO ifc.
ENDLOOP.
xfc-fieldname = 'TOL'.
xfc-datatype = 'NETWR'. "xdetails-type_kind.
APPEND xfc TO ifc.
ENDFORM. "get_structure
&----
*& Form create_dynamic_itab
&----
text
----
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. "create_dynamic_itab
&----
*& Form get_data
&----
text
----
FORM get_data.
Select Data from table.
SELECT * INTO TABLE <dyn_table>
FROM (p_table).
ENDFORM. "get_data
&----
*& Form Write_out
&----
text
----
FORM 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.
ENDFORM. "Write_out
in my program of SD module,I had a field named lab,which did not have fixed values,it picked up values at runtime from TVM1T table
Hope it helps!!!!!!!!!!!!!
2007 Feb 24 2:59 PM
Hi,
I just had another example for u,hope one of them is of some help.
data : it_tvm1t like tvm1t occurs 0 with header line,
l_lines type i.
select * appending corresponding fields of table it_tvm1t from tvm1t.
describe table it_tvm1t lines l_lines.
do l_lines times.
data : l1_no(3) type n,
l1_no1(6) type c,
l1_fld(25).
l1_no = l1_no + 1.
l1_no1 = l1_no.
CONDENSE l1_no1.
concatenate 'LAB' l1_no1 into l1_fld.
read table it_tvm1t with key mvgr1 = l1_no1.
wa_fieldcat-fieldname = l1_fld.
wa_fieldcat-ref_table = 'IT_FINAL'.
wa_fieldcat-ref_field = 'LAB001'.
wa_fieldcat-coltext = it_tvm1t-bezei.
wa_fieldcat-col_pos = 1.
wa_fieldcat-do_sum = 'X'.
APPEND wa_fieldcat TO gt_fieldcat.
enddo.
2007 Feb 26 5:17 AM
Hi Vishnu,
I have pasted a part of my code where i have used dynamic internal tables.Kindly check this below :
ENDIF.
prepare dynamic table for the first parameter
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <itab_first>.
ASSIGN COMPONENT <key1> OF STRUCTURE
l_wa_ldb_result TO <wa_first>.
LOOP AT g_tab_ldb_result INTO l_wa_ldb_result.
APPEND <wa_first> TO <itab_first>.
ENDLOOP.
SORT <itab_first> ASCENDING .
DELETE ADJACENT DUPLICATES FROM <itab_first> COMPARING ALL FIELDS.
2007 Feb 26 5:32 AM
REPORT zzz_test1
NO STANDARD PAGE HEADING
MESSAGE-ID zcs_c2c_001.
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.
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-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.
form write_out.
* 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.
endform.Reward if this helps.
2007 Feb 26 10:47 AM
hi,
Creating a Dynamic Internal Table.
************Creating Dynamic internal table************
parameter p_table type tabname.
field-symbols <tab> type table.
field-symbols <tab1> type any.
types: begin of itab,
t_name type tabname,
t_ref type ref to data,
end of itab.
data itab1 type table of itab with non-unique key t_name.
perform fetch_data using p_table.
perform print_table using p_table.
&----
*& Form fetch_data
&----
text
----
-->P_P_TABLE text
----
FORM fetch_data USING P_TABLE1 type tabname.
data itab2 type itab.
itab2-t_name = p_table1.
create data itab2-t_ref type table of (itab2-t_name) .
assign itab2-t_ref->* to <tab>.
append itab2 to itab1.
select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.
ENDFORM. " fetch_data
&----
*& Form print_table
&----
text
----
-->P_P_TABLE text
----
FORM print_table USING P_TABLE1 type tabname.
DATA t_ref1 TYPE REF TO data.
DATA itab2 TYPE itab.
FIELD-SYMBOLS <field> TYPE ANY.
READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.
ASSIGN itab2-t_ref->* TO <tab>.
CREATE DATA t_ref1 LIKE LINE OF <tab>.
ASSIGN t_ref1->* TO <tab1>.
DO.
*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.
READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.
*WRITE:/ p_table1.
NEW-LINE.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: <field>,' '.
ENDDO.
ENDDO.
ENDFORM. " print_table
do reward if it helps,
priya.
2007 Mar 06 7:45 AM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |