2006 Jun 15 7:52 AM
I have a requirement to declare an internal table dynamically.
Example
data: itab like (CVDIMENTION) occurs 0.
here cvdimantion is table name it will vary dynamically
2006 Jun 15 9:09 AM
REPORT yzz_test06 .
TYPES: BEGIN OF t_data,
key1(5),
key2(5),
key3(5),
period(8),
END OF t_data.
DATA: it_data TYPE TABLE OF t_data,
wa_data TYPE t_data.
DATA: lt_data TYPE TABLE OF t_data,
lw_data TYPE t_data.
DATA: ok_code LIKE sy-ucomm,
g_container TYPE scrfname VALUE 'BCALV_GRID_DEMO_0100_CONT1',
grid1 TYPE REF TO cl_gui_alv_grid,
g_custom_container TYPE REF TO cl_gui_custom_container.
*
DATA: gt_fieldcat TYPE lvc_t_fcat.
DATA: gp_table TYPE REF TO data.
FIELD-SYMBOLS: <gt_table> TYPE table.
PERFORM fill_data.
PERFORM fieldcat_build.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_fieldcat
IMPORTING
ep_table = gp_table
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 gp_table->* TO <gt_table>.
PERFORM fill_table.
CALL SCREEN 100.
----
MODULE PBO OUTPUT *
----
MODULE pbo OUTPUT.
SET PF-STATUS '0100'.
IF g_custom_container IS INITIAL.
CREATE OBJECT g_custom_container
EXPORTING container_name = g_container.
CREATE OBJECT grid1
EXPORTING i_parent = g_custom_container.
CALL METHOD grid1->set_table_for_first_display
CHANGING
it_fieldcatalog = gt_fieldcat
it_outtab = <gt_table>.
ENDIF.
ENDMODULE. "pbo OUTPUT
----
MODULE PAI INPUT *
----
MODULE pai INPUT.
CASE ok_code.
WHEN 'EXIT'.
PERFORM exit_program.
WHEN OTHERS.
do nothing
ENDCASE.
CLEAR ok_code.
ENDMODULE. "pai INPUT
----
FORM fill_table *
----
FORM fill_table.
DATA: l_row TYPE sy-index,
l_key(16).
FIELD-SYMBOLS: <ls_table>.
FIELD-SYMBOLS: <l_field>.
ASSIGN LOCAL COPY OF INITIAL LINE OF <gt_table> TO <ls_table>.
SORT it_data BY key1.
LOOP AT it_data INTO wa_data.
ASSIGN COMPONENT 'MATERIAL'
OF STRUCTURE <ls_table> TO <l_field>.
<l_field> = wa_data-key1.
ASSIGN COMPONENT wa_data-period
OF STRUCTURE <ls_table> TO <l_field>.
<l_field> = wa_data-key2.
AT END OF key1.
APPEND <ls_table> TO <gt_table>.
CLEAR <ls_table>.
ENDAT.
MOVE wa_data-key1 TO l_key.
ENDLOOP.
ENDFORM. "fill_table
----
FORM fieldcat_build *
----
FORM fieldcat_build.
DATA: ls_fieldcat TYPE lvc_s_fcat.
DATA: lv_period(10) TYPE c.
CLEAR: wa_data,
lw_data.
SORT it_data BY period.
LOOP AT it_data INTO wa_data.
lw_data-key1 = wa_data-key1.
lw_data-period = wa_data-period.
APPEND lw_data TO lt_data.
CLEAR lw_data.
ENDLOOP.
SORT lt_data BY period.
DELETE ADJACENT DUPLICATES FROM lt_data COMPARING period.
ls_fieldcat-fieldname = 'MATERIAL'.
ls_fieldcat-inttype = 'C'.
ls_fieldcat-coltext = 'MATERIAL'.
ls_fieldcat-outputlen = 8.
APPEND ls_fieldcat TO gt_fieldcat.
LOOP AT lt_data INTO lw_data.
MOVE lw_data-period TO lv_period.
ls_fieldcat-fieldname = lv_period.
ls_fieldcat-inttype = 'C'.
ls_fieldcat-coltext = lv_period.
ls_fieldcat-outputlen = 8.
APPEND ls_fieldcat TO gt_fieldcat.
ENDLOOP.
ENDFORM. "fieldcat_build
----
FORM EXIT_PROGRAM *
----
FORM exit_program.
CALL METHOD g_custom_container->free.
CALL METHOD cl_gui_cfw=>flush.
LEAVE PROGRAM.
ENDFORM. "exit_program
&----
*& Form fill_data
&----
FORM fill_data .
CLEAR wa_data.
MOVE '00001' TO wa_data-key1.
MOVE 'ZEROONE' TO wa_data-key2.
MOVE 'NOV05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00001' TO wa_data-key1.
MOVE 'ZEROONE' TO wa_data-key2.
MOVE 'JAN05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00002' TO wa_data-key1.
MOVE 'ZEROTWO' TO wa_data-key2.
MOVE 'JAN05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00003' TO wa_data-key1.
MOVE 'ZEROTRI' TO wa_data-key2.
MOVE 'SEP05' TO wa_data-period.
APPEND wa_data TO it_data.
ENDFORM. " fill_data
2006 Jun 15 7:53 AM
Hi ravikumar,
1.
For this purpose,
in my program,
there is an INDEPENDENT FORM
whose inputs are
TABLE NAME / STRUCTURE NAME
and from those, it consructs dynamic table.
2. Here is the program.
the dynamic table name will be
<DYNTABLE>.
3. U can use this program (FORM in this program)
to generate any kind of internal table
by specifying TABLE NAME .
4.
REPORT abc.
*----
COMPULSORY
FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.
FIELD-SYMBOLS: <dynline> TYPE ANY.
DATA: lt TYPE lvc_t_fcat.
DATA: ls TYPE lvc_s_fcat.
FIELD-SYMBOLS: <fld> TYPE ANY.
DATA : fldname(50) TYPE c.
*----
parameters : iname LIKE dd02l-tabname.
*----
START-OF-SELECTION.
*----
PERFORM
PERFORM mydyntable USING lt.
BREAK-POINT.
*----
INDEPENDENT FORM
*----
FORM mydyntable USING ptabname.
*----
Create Dyn Table From FC
FIELD-SYMBOLS: <fs_data> TYPE REF TO data.
FIELD-SYMBOLS: <fs_1>.
FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.
DATA: lt_data TYPE REF TO data.
data : lt TYPE lvc_t_fcat .
DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.
*----
CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'
EXPORTING
tabname = iname
TABLES
ddfields = ddfields.
.
*----
CONSTRUCT FIELD LIST
LOOP AT ddfields.
ls-fieldname = ddfields-fieldname.
APPEND ls TO lt.
ENDLOOP.
ASSIGN lt_data TO <fs_data>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
*----
Assign Dyn Table To Field Sumbol
ASSIGN <fs_data>->* TO <fs_1>.
ASSIGN <fs_1> TO <fs_2>.
ASSIGN <fs_1> TO <dyntable>.
ENDFORM. "MYDYNTABLE
regards,
amit m.
2006 Jun 15 7:54 AM
use method CREATE_DYNAMIC_TABLE Of class CL_ALV_TABLE_CREATE to create itab at different points in program based on a logic.
2006 Jun 15 7:56 AM
This has been discussed many times in this forum. Please check out this weblog.
/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap
2006 Jun 15 9:09 AM
REPORT yzz_test06 .
TYPES: BEGIN OF t_data,
key1(5),
key2(5),
key3(5),
period(8),
END OF t_data.
DATA: it_data TYPE TABLE OF t_data,
wa_data TYPE t_data.
DATA: lt_data TYPE TABLE OF t_data,
lw_data TYPE t_data.
DATA: ok_code LIKE sy-ucomm,
g_container TYPE scrfname VALUE 'BCALV_GRID_DEMO_0100_CONT1',
grid1 TYPE REF TO cl_gui_alv_grid,
g_custom_container TYPE REF TO cl_gui_custom_container.
*
DATA: gt_fieldcat TYPE lvc_t_fcat.
DATA: gp_table TYPE REF TO data.
FIELD-SYMBOLS: <gt_table> TYPE table.
PERFORM fill_data.
PERFORM fieldcat_build.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_fieldcat
IMPORTING
ep_table = gp_table
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 gp_table->* TO <gt_table>.
PERFORM fill_table.
CALL SCREEN 100.
----
MODULE PBO OUTPUT *
----
MODULE pbo OUTPUT.
SET PF-STATUS '0100'.
IF g_custom_container IS INITIAL.
CREATE OBJECT g_custom_container
EXPORTING container_name = g_container.
CREATE OBJECT grid1
EXPORTING i_parent = g_custom_container.
CALL METHOD grid1->set_table_for_first_display
CHANGING
it_fieldcatalog = gt_fieldcat
it_outtab = <gt_table>.
ENDIF.
ENDMODULE. "pbo OUTPUT
----
MODULE PAI INPUT *
----
MODULE pai INPUT.
CASE ok_code.
WHEN 'EXIT'.
PERFORM exit_program.
WHEN OTHERS.
do nothing
ENDCASE.
CLEAR ok_code.
ENDMODULE. "pai INPUT
----
FORM fill_table *
----
FORM fill_table.
DATA: l_row TYPE sy-index,
l_key(16).
FIELD-SYMBOLS: <ls_table>.
FIELD-SYMBOLS: <l_field>.
ASSIGN LOCAL COPY OF INITIAL LINE OF <gt_table> TO <ls_table>.
SORT it_data BY key1.
LOOP AT it_data INTO wa_data.
ASSIGN COMPONENT 'MATERIAL'
OF STRUCTURE <ls_table> TO <l_field>.
<l_field> = wa_data-key1.
ASSIGN COMPONENT wa_data-period
OF STRUCTURE <ls_table> TO <l_field>.
<l_field> = wa_data-key2.
AT END OF key1.
APPEND <ls_table> TO <gt_table>.
CLEAR <ls_table>.
ENDAT.
MOVE wa_data-key1 TO l_key.
ENDLOOP.
ENDFORM. "fill_table
----
FORM fieldcat_build *
----
FORM fieldcat_build.
DATA: ls_fieldcat TYPE lvc_s_fcat.
DATA: lv_period(10) TYPE c.
CLEAR: wa_data,
lw_data.
SORT it_data BY period.
LOOP AT it_data INTO wa_data.
lw_data-key1 = wa_data-key1.
lw_data-period = wa_data-period.
APPEND lw_data TO lt_data.
CLEAR lw_data.
ENDLOOP.
SORT lt_data BY period.
DELETE ADJACENT DUPLICATES FROM lt_data COMPARING period.
ls_fieldcat-fieldname = 'MATERIAL'.
ls_fieldcat-inttype = 'C'.
ls_fieldcat-coltext = 'MATERIAL'.
ls_fieldcat-outputlen = 8.
APPEND ls_fieldcat TO gt_fieldcat.
LOOP AT lt_data INTO lw_data.
MOVE lw_data-period TO lv_period.
ls_fieldcat-fieldname = lv_period.
ls_fieldcat-inttype = 'C'.
ls_fieldcat-coltext = lv_period.
ls_fieldcat-outputlen = 8.
APPEND ls_fieldcat TO gt_fieldcat.
ENDLOOP.
ENDFORM. "fieldcat_build
----
FORM EXIT_PROGRAM *
----
FORM exit_program.
CALL METHOD g_custom_container->free.
CALL METHOD cl_gui_cfw=>flush.
LEAVE PROGRAM.
ENDFORM. "exit_program
&----
*& Form fill_data
&----
FORM fill_data .
CLEAR wa_data.
MOVE '00001' TO wa_data-key1.
MOVE 'ZEROONE' TO wa_data-key2.
MOVE 'NOV05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00001' TO wa_data-key1.
MOVE 'ZEROONE' TO wa_data-key2.
MOVE 'JAN05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00002' TO wa_data-key1.
MOVE 'ZEROTWO' TO wa_data-key2.
MOVE 'JAN05' TO wa_data-period.
APPEND wa_data TO it_data.
CLEAR wa_data.
MOVE '00003' TO wa_data-key1.
MOVE 'ZEROTRI' TO wa_data-key2.
MOVE 'SEP05' TO wa_data-period.
APPEND wa_data TO it_data.
ENDFORM. " fill_data
2006 Jun 15 9:15 AM
Hai Ravi
HI
PLEASE GO THROUGH THE LINKS
How to create a Dynamic Internal Table?
Dynamic Internal Tables
You can put a check depending on your condition you can pass the table parameters to the FM..Ex:-If condition1 is true. Perform gui_upload tables ITAB1.elseif condition2 is true. Perform gui_upload tables ITAB2.endif.Form gui_upload tables ITAB structure ....
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LDF_FILENAME3 FILETYPE = LDF_TYPE1 HAS_FIELD_SEPARATOR = 'X' TABLES DATA_TAB = ITAB
EXCEPTIONS
FILE_OPEN_ERROR = 1 FILE_READ_ERROR = 2 NO_BATCH = 3 GUI_REFUSE_FILETRANSFER = 4 INVALID_TYPE = 5 NO_AUTHORITY = 6 UNKNOWN_ERROR = 7 BAD_DATA_FORMAT = 8 HEADER_NOT_ALLOWED = 9 SEPARATOR_NOT_ALLOWED = 10 HEADER_TOO_LONG = 11 UNKNOWN_DP_ERROR = 12 ACCESS_DENIED = 13 DP_OUT_OF_MEMORY = 14 DISK_FULL = 15 DP_TIMEOUT = 16 OTHERS = 17 .
IF SY-SUBRC <> 0.
MESSAGE E506 WITH 'UPLOAD' 'SY-SUBRC=' SY-SUBRC SPACE. ENDIF.
IF THIS IS USEFUL PLEASE REWARD
Thanks & regards
Sreenivasulu P
2006 Jun 15 9:16 AM
2006 Jun 15 9:36 AM