‎2006 Dec 27 2:50 PM
Hello,
I have a program with a parameter for a table name.
PARAMETERS: pa_tab TYPE dd02l-tabname.
This parameter will be filled with a dictionary table name.
I am trying to define a type using this parameter name like this:
TYPES: BEGIN OF t_itab.
INCLUDE structure (pa_tab).
TYPES cellstyles TYPE lvc_t_styl.
TYPES END OF t_itab.
But it is not working. Any ideeas?
Thank you!
‎2006 Dec 27 3:16 PM
Hi George
You will not be able to pass a dynamic value to TYPE as shown below. The program will throw a syntax error.
TYPES: BEGIN OF t_itab.
* The strucure name is not valid as the value will be passed only at run time
INCLUDE structure (pa_tab).
TYPES cellstyles TYPE lvc_t_styl.
TYPES END OF t_itab.
Whereas you can create a field catalogue for your dynamic internal table and process the data as shown above.
But you can pass the dynamic table names to your select query at run time
* Select Data from table.
SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>
FROM (p_table).
Regards
Kathirvel
‎2006 Dec 27 2:54 PM
You can try this option,
Option - 1
REPORT zkb_dyn_itab.
PARAMETERS : p_table(30) TYPE c DEFAULT 'T000' OBLIGATORY.
DATA: o_dref TYPE REF TO data.
FIELD-SYMBOLS: <fs_table> TYPE ANY TABLE,
<fs_warea> TYPE ANY,
<fs_field> TYPE ANY.
START-OF-SELECTION.
CREATE DATA o_dref TYPE TABLE OF (p_table).
ASSIGN o_dref->* TO <fs_table>.
SELECT * FROM (p_table) INTO TABLE <fs_table>.
IF sy-subrc EQ 0.
LOOP AT <fs_table> ASSIGNING <fs_warea>.
ASSIGN COMPONENT 1 OF STRUCTURE <fs_warea> TO <fs_field>.
WRITE / <fs_field>.
ASSIGN COMPONENT 2 OF STRUCTURE <fs_warea> TO <fs_field>.
WRITE / <fs_field>.
ENDLOOP.
ELSE.
MESSAGE 'Error selecting data!' TYPE 'I'.
ENDIF.
Option 2
REPORT zkb_dynamic_itab.
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 'SCARR'.
SELECTION-SCREEN END OF BLOCK b1.
START-OF-SELECTION.
PERFORM get_structure.
PERFORM create_dynamic_itab.
PERFORM get_data.
PERFORM read_data_from_dyntable USING 'JL'.
*&---------------------------------------------------------------------*
*& 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[].
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. "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 CORRESPONDING FIELDS OF TABLE <dyn_table>
FROM (p_table).
ENDFORM. "get_data
*&---------------------------------------------------------------------*
*& Form read_data_from_dyntable
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_CARRID text
*----------------------------------------------------------------------*
FORM read_data_from_dyntable USING p_carrid TYPE s_carr_id.
FIELD-SYMBOLS: <l_dyn_wa> TYPE ANY,
<l_dyn_field> TYPE ANY.
DATA: l_dy_line TYPE REF TO data.
DATA: li_results TYPE match_result_tab,
lw_results TYPE match_result.
CREATE DATA l_dy_line LIKE LINE OF <dyn_table>.
ASSIGN l_dy_line->* TO <l_dyn_wa>.
ASSIGN COMPONENT 2 OF STRUCTURE <l_dyn_wa> TO <l_dyn_field>.
<l_dyn_field> = p_carrid.
* Reading the data
FIND ALL OCCURRENCES OF <l_dyn_field>
IN TABLE <dyn_table>
RESULTS li_results.
READ TABLE li_results INTO lw_results INDEX 1.
READ TABLE <dyn_table> INTO <dyn_wa> INDEX lw_results-line.
WRITE:/ <dyn_wa>.
* Modifying the data
ASSIGN COMPONENT 5 OF STRUCTURE <dyn_wa> TO <l_dyn_field>.
<l_dyn_field> = 'http://www.jal.com'.
WRITE:/ <dyn_wa>.
ENDFORM. " read_data_from_dyntable
Regards
Kathirvel
‎2006 Dec 27 3:05 PM
Thank you for your example. But I just want to define an internal table with the structure of pa_tab plus the TYPE lvc_t_styl. Does it has to be so complicated?
‎2006 Dec 27 4:24 PM
Hello George,
the ABAP runtime is a byte code interpreter not a source code interpretor. As consequence a program cannot be altered during execution nor can the byte code be influenced by parameter. Any declarative statement and especially include <file> or include <type> is resolved at compile time only.
So yes it is necessary to do it hard way. If you need to do it frequently maybe moving common patterns to a kind of model class may hide away some complexity within your reports.
Regards,
Klaus
‎2006 Dec 27 3:16 PM
Hi George
You will not be able to pass a dynamic value to TYPE as shown below. The program will throw a syntax error.
TYPES: BEGIN OF t_itab.
* The strucure name is not valid as the value will be passed only at run time
INCLUDE structure (pa_tab).
TYPES cellstyles TYPE lvc_t_styl.
TYPES END OF t_itab.
Whereas you can create a field catalogue for your dynamic internal table and process the data as shown above.
But you can pass the dynamic table names to your select query at run time
* Select Data from table.
SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>
FROM (p_table).
Regards
Kathirvel
‎2006 Dec 28 9:21 AM
Thank you for your answers. I know now that I have to define a field catalog to create dynamically the pa_tab table. But after I add the fields of pa_tab to the fieldcatalog, how can I define a field 'CELLSTYLES' type LVC_T_STYL (table) to the fieldcatalog? I need that my dynamic table to be used with an ALV grid, and I need that CELLSTYLES field.
Thanks!