Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Create Table Type Dynamically

vivek_gaurav1
Participant
0 Likes
10,539

Hi All,

I need to create Table Type dynamically based on existing structure..

For that i followed the link: https://scn.sap.com/thread/1725739

but in this , there is no input parameter for table type name.. I am sending structure name.

sample code given below:

TRY.

DATA: r_type_table  TYPE REF TO cl_abap_tabledescr,

       r_type_struct TYPE REF TO cl_abap_structdescr,

       ls_struc_type TYPE REF TO CL_ABAP_DATADESCR,

       ls_line_type TYPE REF TO CL_ABAP_TYPEDESCR,

       LS_P_RESULT TYPE REF TO CL_ABAP_TABLEDESCR.

       ls_line_type = cl_abap_structdescr=>DESCRIBE_BY_NAME( 'zst_cr_salesrep' ).

       r_type_struct ?= ls_line_type.

       r_type_table = cl_abap_tabledescr=>create( r_type_struct ).

     CATCH cx_sy_table_creation .

ENDTRY.


Also i am not getting any error.


Kindly advice..


Thanks

Vivek

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,690

Hi,

what exactly do you want to do?

The sample code you posted is an example how to create a local table type so you can use this type in your methods. Therefore you don't need a name for the table type.

If you want to create a table type in the DDIC, you won't be able to use this code snippet. Then you would have to use FM DDIF_TTYP_PUT and DDIF_TTYP_ACTIVATE.

Regards,

Rebekka

5 REPLIES 5
Read only

Former Member
0 Likes
4,691

Hi,

what exactly do you want to do?

The sample code you posted is an example how to create a local table type so you can use this type in your methods. Therefore you don't need a name for the table type.

If you want to create a table type in the DDIC, you won't be able to use this code snippet. Then you would have to use FM DDIF_TTYP_PUT and DDIF_TTYP_ACTIVATE.

Regards,

Rebekka

Read only

former_member241258
Active Participant
0 Likes
4,690

hi

below code will help u.

REPORT ZDEMO2.

DATA:GT_DATA TYPE REF TO DATA.

FIELD-SYMBOLS:<GT_FINAL> TYPE STANDARD TABLE.


START-OF-SELECTION.

CREATE DATA GT_DATA TYPE VBAK." HERE MENTIONED EXISTING STRUCTURE NAME.
      
ASSIGN GT_DATA->* TO <GT_FINAL>. 

Read only

former_member241258
Active Participant
0 Likes
4,690

Above code is wrong.

use this

REPORT ZDEMO2.

DATA:GT_DATA TYPE REF TO DATA.

FIELD-SYMBOLS:<GT_FINAL> TYPE STANDARD TABLE.


START-OF-SELECTION.

CREATE DATA GT_DATA TYPE STANDARD TABLE OF VBAK." HERE MENTIONED EXISTING STRUCTURE NAME.

ASSIGN GT_DATA->* TO <GT_FINAL>.

IF 1 = 4.

ENDIF.

Read only

Former Member
0 Likes
4,690

Check This:

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

               <table_out> 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.

START-OF-SELECTION.

  PERFORM get_structure.

  PERFORM create_dynamic_itab.

*******Creates a dynamic internal table*********

  PERFORM get_data.

FORM get_structure.

  DATA : idetails TYPE abap_compdescr_tab.

  DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

  ref_table_des ?= cl_abap_typedescr=>describe_by_name( p_table ).

  idetails[] = ref_table_des->components[].

ENDFORM.                    "get_structure

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

      i_length_in_byte = 'X'

    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.

* Select Data from table.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>

             UP TO p_num ROWS

             FROM (p_table).

  IF sy-subrc EQ 0.

    SORT <dyn_table>.

  ENDIF.

ENDFORM

Read only

former_member241258
Active Participant
0 Likes
4,690

hi

we modified ur code see below plz

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
                <DYN_WA> TYPE ANY.


DATA: DY_TABLE TYPE REF TO DATA,
       DY_LINE  TYPE REF TO DATA,
       XFC TYPE LVC_S_FCAT,
       IFC TYPE LVC_T_FCAT.


START-OF-SELECTION.

   PERFORM GET_STRUCTURE.

   PERFORM CREATE_DYNAMIC_ITAB.

   PERFORM get_data.





FORM GET_STRUCTURE.

CREATE DATA DY_TABLE TYPE STANDARD TABLE OF (P_TABLE).

CREATE DATA DY_LINE TYPE (P_TABLE).

ENDFORM.                    "get_structure


FORM CREATE_DYNAMIC_ITAB.

   ASSIGN DY_TABLE->* TO <DYN_TABLE>.
   ASSIGN DY_LINE->* TO <DYN_WA>.

ENDFORM.                    "create_dynamic_itab

FORM GET_DATA.

   SELECT * FROM (P_TABLE) INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE>
              UP TO P_NUM ROWS.

   IF SY-SUBRC = 0.
     SORT <DYN_TABLE>.
   ENDIF.

ENDFORM