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

Dynamic Structure

Former Member
0 Likes
1,254

Hi!

Does anyone know a way to create a structure from the content of the first line from an internal table (type string).

I have to do this after uploading an excel-sheet with fieldnames as header to compare the fields of the uploaded table with the fields of the db-table.

Thanx!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
872

Hi,

U can try this way : First create a fieldcatalog with say 100 fields like field1, field2, field2......field100.

do100 times.

concatenate 'field' sy-index into fieldname.

build fieldcatalogue.

enddo.

Now create ur internal table with that fieldcatalog and upload ur excel sheet by passing that internal table using any of the upload FM like ALSM_EXCEL_TO_INTERNAL_TABLE . Now read the first row of the internal table which is having ur fieldnames of ur DB table. But Fixing the no of fields should be decided based on ur requirement.

-Hope it helps.

SatyaPriya

3 REPLIES 3
Read only

Former Member
0 Likes
872

Hi,

Here is the link to create the Dynamic internal table

http://www.sap-img.com/ab030.htm

Regards

Sudheer

Read only

Former Member
0 Likes
873

Hi,

U can try this way : First create a fieldcatalog with say 100 fields like field1, field2, field2......field100.

do100 times.

concatenate 'field' sy-index into fieldname.

build fieldcatalogue.

enddo.

Now create ur internal table with that fieldcatalog and upload ur excel sheet by passing that internal table using any of the upload FM like ALSM_EXCEL_TO_INTERNAL_TABLE . Now read the first row of the internal table which is having ur fieldnames of ur DB table. But Fixing the no of fields should be decided based on ur requirement.

-Hope it helps.

SatyaPriya

Read only

uwe_schieferstein
Active Contributor
0 Likes
872

Hello Andreas

Since you want to compare your Excel data to DB table data the columns of your Excel-sheet must somehow map to the corresponding table field. However, the order of the columns in the Excel-sheet may be different from the order of the table fields. In this case you could dynamically define a structure and use the MOVE-CORRESPONDING statement to move them into the corresponding DDIC structure.

Example: Your DB table (ZTABLE) has the fields FIELD1, FIELD2, FIELD3. Your Excel-sheet contains the data as columns FIELD2, FIELD3, FIELD1.

(1) Dynamically create a structure based on the column order in your excel (see coding below) -> gs_struc_excel

(2) Split the Excel-sheet record (string) into its parts and store in gs_struc_excel

(3) Collect record into itab with DDIC structure

DATA:  gs_struc_db  TYPE ztable,
            gt_itab          TYPE STANDARD TABLE OF ztable.

  MOVE-CORRESPONDING gs_struc_excel TO gs_strc_db.
  APPEND gs_struc_db TO gt_itab.

(4) Compare with data from DB table.

f you are already on SAP release 6.40 (or higher) you can use the TYPE HANDLE statement to create any internal table you like.

The simplified version of de-referencing a data reference to a table type field symbol is already available since 6.20 (perhaps on 4.6c, too).

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_RTTI_CREATE_STRUCTURES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
 
REPORT  zus_sdn_rtti_create_structures.
 
 
TYPE-POOLS: abap.
 
 
DATA:
  celltab          TYPE lvc_t_styl.
 
DATA:
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr        TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_data         TYPE REF TO data,
  gdo_handle       TYPE REF TO data,
  gs_comp          TYPE abap_componentdescr,
  gt_components    TYPE abap_component_tab.
*
*    name       TYPE string,
*    type       TYPE REF TO cl_abap_datadescr,
*    as_include TYPE abap_bool,
*    suffix     TYPE string,
 
FIELD-SYMBOLS:
  <gd_fld>      TYPE ANY,
  <gs_struc>    TYPE ANY,
  <gt_itab>     TYPE table.
 
 
PARAMETER:
  p_tabnam      TYPE tabname  DEFAULT 'KNB1'.
 
START-OF-SELECTION.
 
* Create dynamically structure
  CREATE DATA gdo_data TYPE (p_tabnam).
  ASSIGN gdo_data->* TO <gs_struc>.
  CHECK ( <gs_struc> IS ASSIGNED ).
 
 
* Simulate dynamic addition of columns to ALV list
  DO 10 TIMES.
    ASSIGN COMPONENT syst-index OF STRUCTURE <gs_struc> TO <gd_fld>.
 
    CLEAR: gs_comp.
    gs_comp-type ?= cl_abap_datadescr=>describe_by_data( <gd_fld> ).
    gs_comp-name  = gs_comp-type->get_relative_name( ).
    APPEND gs_comp TO gt_components.
 
    go_sdescr  = cl_abap_structdescr=>create( gt_components ).
    go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
    "   Create data refence followed by table creation
    CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
    ASSIGN gdo_handle->* TO <gt_itab>.
 
*   Dynamic select
    SELECT        * FROM  (p_tabnam)
      INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
           WHERE  bukrs  = '2000'.
 
    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = go_table
          CHANGING
            t_table      = <gt_itab>.
        go_table->display( ).
      CATCH cx_salv_msg .
    ENDTRY.
 
  ENDDO.
 
 
  " Add table type as field to structure
  CLEAR: gs_comp.
  gs_comp-type ?= cl_abap_typedescr=>describe_by_data( celltab ).
  gs_comp-name  = 'CELLTAB'.
  APPEND gs_comp TO gt_components.
 
  go_sdescr  = cl_abap_structdescr=>create( gt_components ).
  go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
  CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
  ASSIGN gdo_handle->* TO <gt_itab>.
 
*   Dynamic select
  SELECT        * FROM  (p_tabnam)
    INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
         WHERE  bukrs  = '2000'.
 
 
 
  " Simplified version of table creation:
  CLEAR: gdo_data.
  UNASSIGN <gt_itab>.
 
  CREATE DATA gdo_data TYPE (p_tabnam).
  ASSIGN gdo_data->* TO <gt_itab>.
 
 
END-OF-SELECTION.

Regards

Uwe