‎2007 Apr 18 6:26 AM
Hi,
I have an internal table < DYN_TABLE> , the structure of which will be known at run time. I need to add 2 more fields for this structure .
For example if <DYN_TABLE> contains 8 fields I need to add 2 more fields.
If <DYN_TABLE> contains 10 fields I need to add 2 more fields.
‎2007 Apr 18 6:29 AM
‎2007 Apr 18 6:31 AM
hi
there is an alternative to this.u can create the structure of internal table once with max fields required and then u can use
select * into <b>corresponding fields of table</b> .........
regards
ravish
<b>dont forget to reward points if helpful</b>
‎2007 Apr 18 6:32 AM
Hi
U can't change the structure of an internal table dynamically, u should create a new table with the fields of the old table and the new fields.
U can use the method CREATE_DYNAMIC_TABLE of class CL_ALV_TABLE_CREATE to create a table dynamically.
After creating it you have to transfer all data from old to new table.
code for the same
DATA: MY_TABLE TYPE REF TO DATA.
FIELD-SYMBOLS: <FS_TABLE> TYPE ANY.
DATA: LS_ALV_CAT TYPE LVC_S_FCAT.
DATA: LT_ALV_CAT TYPE LVC_T_FCAT.
LS_ALV_CAT-FIELDNAME = <FIELD NAME>.
LS_ALV_CAT-TABNAME = <INTERNAL TABLE NAME>.
---> here indicate all characteristic of the field or if you have a dictionary reference:
LS_ALV_CAT-REF_FIELD =
LS_ALV_CAT-REF_TABLE =
APPEND LS_ALV_CAT TP LT_ALV_CAT.
...........................
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = LT_ALV_CAT
IMPORTING
EP_TABLE = MY_TABLE.
ASSIGN MY_TABLE->* TO <FS_TABLE>.
‎2007 Apr 18 6:33 AM
One more possible solution
Check out the class "CL_ALV_TABLE_CREATE". Try its method called "CREATE_DYNAMIC_TABLE". It is some easy just prepare a field catalog and call the method to give reference of dynamically created table.
‎2007 Apr 18 6:35 AM
HI naga,
Plz go through this program.
TABLES : tadir. " table in which the obj_name field stores all the
" database table names
TYPE-POOLS : slis.
TYPES ztab LIKE dcobjdef-name .
DATA : dyntab LIKE dntab OCCURS 0 WITH HEADER LINE,
dref TYPE REF TO data,
i_fcat TYPE lvc_t_fcat ,
wa_fcat TYPE lvc_s_fcat ,
w_pgm LIKE sy-repid,
count TYPE i VALUE 0,
w_obj_name LIKE tadir-obj_name.
DATA : wa_fieldcat TYPE slis_fieldcat_alv, "ALV FIELD CATALOG TABLE
i_fieldcat TYPE slis_t_fieldcat_alv, "ALV FIELD CATALOG STRUCTURE
i_event TYPE slis_t_event, "ALV EVENT HANDLING TABLE
wa_event TYPE slis_alv_event, "ALV EVENT HANDLING STRUCT
i_listheader TYPE slis_t_listheader, "ALV LIST HEADER TABLE
wa_listheader TYPE slis_listheader, "ALV LIST HEADER STRUCT
i_layout TYPE slis_layout_alv.
FIELD-SYMBOLS :<newtab> TYPE table.
INITIALIZATION.
w_pgm = sy-repid.
****************SELECTION-SCREEN 2000
SELECTION-SCREEN BEGIN OF SCREEN 2000 AS WINDOW.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE tit1.
PARAMETERS: tab_name TYPE ztab . " Enter database table name
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN END OF SCREEN 2000.
***************END OF SELECTION-SCREEN 2000
tit1 = 'Enter Table Name?'.
START-OF-SELECTION.
CALL SELECTION-SCREEN 2000 STARTING AT 10 10.
PERFORM occurcheck.
*********CHECKING WHETHER THE DATABASE TABLE EXISTS
FORM occurcheck.
SELECT obj_name FROM tadir INTO w_obj_name WHERE obj_name = tab_name.
ENDSELECT.
IF sy-subrc <> 0.
MESSAGE i003."Table not found in the database .
CALL SELECTION-SCREEN 2000 STARTING AT 10 10.
PERFORM occurcheck.
ELSE.
PERFORM startprocess.
EXIT.
ENDIF.
ENDFORM.
**********GETTING THE FIELD NAMES OF THE INPUT DATABASE TABLE
FORM startprocess.
CALL FUNCTION 'NAMETAB_GET'
EXPORTING
langu = sy-langu
tabname = tab_name
TABLES
nametab = dyntab. "dntab now contains the field names
**********FILLING THE CATALOG OF NEW DYNAMIC INTERNAL TABLE
LOOP AT dyntab.
wa_fcat-fieldname = dyntab-fieldname.
wa_fcat-ref_field = dyntab-fieldname.
wa_fcat-ref_table = dyntab-tabname.
APPEND wa_fcat TO i_fcat .
ENDLOOP.
***********CREATING A POINTER (FIELD SYMBOL) TO THE INTERNAL TABLE
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_fcat
IMPORTING
ep_table = dref.
ASSIGN dref->* TO <newtab>.
SELECT * FROM (dyntab-tabname) INTO TABLE <newtab>.
**********CHECKING WHETHER DATA HAS BEEN UPLOADED.
DESCRIBE TABLE <newtab> LINES sy-tfill.
DESCRIBE TABLE <newtab> LINES count.
IF sy-tfill = 0 .
MESSAGE i001. "Internal table is not filled
ELSE.
MESSAGE i002 WITH count. "Internal table is filled
ENDIF.
****************EVENTS USED IN ALV
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 0 " 0-simple list, 1-hierarchial list.
IMPORTING
et_events = i_event.
SORT i_event.
READ TABLE i_event
WITH KEY name = slis_ev_top_of_page "TOP_OF_PAGE event
INTO wa_event.
IF sy-subrc = 0.
MOVE 'IAM_TOP_OF_PAGE' TO wa_event-form. "IAM_TOP_OF_PAGE will
"call subroutine automatically
MODIFY i_event FROM wa_event INDEX sy-tabix.
ENDIF.
****************DISPLAYING ALV.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = w_pgm
i_structure_name = dyntab-tabname
it_events = i_event
TABLES
t_outtab = <newtab>.
ENDFORM.
**********FORM TOP_OF-PAGE.
******FILLING OF LIST HEADER TABLE
FORM iam_top_of_page.
CLEAR wa_listheader.
wa_listheader-typ = 'H'. "HEADING
wa_listheader-info = 'ALV CREATED DYNAMICALLY'.
APPEND wa_listheader TO i_listheader.
CLEAR wa_listheader.
wa_listheader-typ = 'S'. "SUB-HEADING
wa_listheader-key = 'PGM NAME:'.
wa_listheader-info = w_pgm.
APPEND wa_listheader TO i_listheader.
CLEAR wa_listheader.
wa_listheader-typ = 'S'.
wa_listheader-key = 'USER:'.
wa_listheader-info = sy-uname.
APPEND wa_listheader TO i_listheader.
CLEAR wa_listheader.
wa_listheader-typ = 'S'.
wa_listheader-key = 'DATE:'.
wa_listheader-info = sy-datum.
APPEND wa_listheader TO i_listheader.
CLEAR wa_listheader.
wa_listheader-typ = 'S'.
wa_listheader-key = 'TABLE NAME:'.
wa_listheader-info = dyntab-tabname.
APPEND wa_listheader TO i_listheader.
********FUNCTION TO WRITE THE TITLE AND SUB-TITLE OF THE ALV.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = i_listheader.
ENDFORM.
Rewords some points,
Rgds,
P.Nag