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 Internal Table creation and population

Former Member
0 Likes
1,240

Hi gurus !

my issue refers to the slide 10 provided in this slideshow : https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b735e96f...

My example is gonna sound dumb, but anyway: I want to dynamically select from a table into a dynamically created itab.

Letu2019s use only EKPO, and only field MENGE.

For this, I use Classes cl_abap_elemdescr, cl_sql_result_set and the Data Ref for table creation. But while fetching the resultset, program dumps when fields like MENGE, WRBTR are accessed. Obviously their type are not correctly taken into account by my program.

Here it comes:


DATA: element_ref             TYPE REF TO cl_abap_elemdescr,
	 vl_fieldname	          TYPE string,
       	 tl_components         TYPE abap_component_tab,
       	 sl_components         LIKE LINE OF tl_components_alv,
 linetype_lcl          	TYPE REF TO cl_abap_structdescr,
 ty_table_type            TYPE REF TO cl_abap_tabledescr,
 g_resultset   		TYPE REF TO cl_sql_result_set

u2026

CONCATENATE sg_columns-table_name '-' sg_columns-column_name INTO vl_fieldname.
* sg_columns-table_name contains 'EKPO'
* sg_columns-column_name contains 'MENGE'


* getting the element as a component
element_ref ?= cl_abap_elemdescr=>describe_by_name( vl_fieldname ).

sl_components-name  = sg_columns-column_name.
sl_components-type ?= element_ref.

APPEND sl_components TO tl_components.


* dynamic creation of internal table
linetype_lcl = cl_abap_structdescr=>create( tl_components ).

ty_table_type = cl_abap_tabledescr=>create(
                  p_line_type = linetype_lcl ).


u2026


* Then I will create my field symbol table and line. Code has been cut here.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.

u2026

* Then I will execute my query. Here itu2019s: Select MENGE From EKPO Where Rownum = 1.
  g_resultset = g_stmt_ref->execute_query( stmt_str ).

* Then structure for the Resultset is set
  CALL METHOD g_resultset->set_param_struct
    EXPORTING
      struct_ref = dy_line.


* Fetching the lines of the resultset  => Dumpu2026
  WHILE g_resultset->next( ) > 0.
    ASSIGN dy_line->* TO <dyn_wa>.
    APPEND <dyn_wa> TO <dyn_table>.
  ENDWHILE.

Anyone has any clue to how prevent my Dump ??

The component for MENGE seems to be described as a P7 with 2 decimals. And the resultset wanna use a QUAN type... or something like that !

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,037

Hello

I have expanded your sample coding for selecting three fields out of EKPO:


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_SQL_RESULT_SET
*&
*&---------------------------------------------------------------------*
*& Thread: Dynamic Internal Table creation and population
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1375510"></a>
*&
*& NOTE: Coding for dynamic structure / itab creation taken from:
*& Creating Flat and Complex Internal Tables Dynamically using RTTI
*& <a href="https://wiki.sdn.sap.com/wiki/display/Snippets/Creating+Flat+and+" TARGET="test_blank">https://wiki.sdn.sap.com/wiki/display/Snippets/Creating+Flat+and+</a>
*& Complex+Internal+Tables+Dynamically+using+RTTI
*&---------------------------------------------------------------------*

REPORT  zus_sdn_sql_result_set.

TYPE-POOLS: abap.

DATA:
 go_sql_stmt       TYPE REF TO cl_sql_statement,
 go_resultset      TYPE REF TO cl_sql_result_set,
 gd_sql_clause     TYPE string.

DATA:
  gd_tabfield      TYPE string,
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr_new    TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_handle       TYPE REF TO data,
  gdo_record       TYPE REF TO data,
  gs_comp          TYPE abap_componentdescr,
  gt_components    TYPE abap_component_tab.

FIELD-SYMBOLS:
  <gs_record>   TYPE ANY,
  <gt_itab>     TYPE STANDARD TABLE.

START-OF-SELECTION.

continued.

Hi gurus !

my issue refers to the slide 10 provided in this slideshow : https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b735e96f...

My example is gonna sound dumb, but anyway: I want to dynamically select from a table into a dynamically created itab.

Letu2019s use only EKPO, and only field MENGE.

For this, I use Classes cl_abap_elemdescr, cl_sql_result_set and the Data Ref for table creation. But while fetching the resultset, program dumps when fields like MENGE, WRBTR are accessed. Obviously their type are not correctly taken into account by my program.

Here it comes:


DATA: element_ref             TYPE REF TO cl_abap_elemdescr,
	 vl_fieldname	          TYPE string,
       	 tl_components         TYPE abap_component_tab,
       	 sl_components         LIKE LINE OF tl_components_alv,
 linetype_lcl          	TYPE REF TO cl_abap_structdescr,
 ty_table_type            TYPE REF TO cl_abap_tabledescr,
 g_resultset   		TYPE REF TO cl_sql_result_set

u2026

CONCATENATE sg_columns-table_name '-' sg_columns-column_name INTO vl_fieldname.
* sg_columns-table_name contains 'EKPO'
* sg_columns-column_name contains 'MENGE'


* getting the element as a component
element_ref ?= cl_abap_elemdescr=>describe_by_name( vl_fieldname ).

sl_components-name  = sg_columns-column_name.
sl_components-type ?= element_ref.

APPEND sl_components TO tl_components.


* dynamic creation of internal table
linetype_lcl = cl_abap_structdescr=>create( tl_components ).

ty_table_type = cl_abap_tabledescr=>create(
                  p_line_type = linetype_lcl ).


u2026


* Then I will create my field symbol table and line. Code has been cut here.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.

u2026

* Then I will execute my query. Here itu2019s: Select MENGE From EKPO Where Rownum = 1.
  g_resultset = g_stmt_ref->execute_query( stmt_str ).

* Then structure for the Resultset is set
  CALL METHOD g_resultset->set_param_struct
    EXPORTING
      struct_ref = dy_line.


* Fetching the lines of the resultset  => Dumpu2026
  WHILE g_resultset->next( ) > 0.
    ASSIGN dy_line->* TO <dyn_wa>.
    APPEND <dyn_wa> TO <dyn_table>.
  ENDWHILE.

Anyone has any clue to how prevent my Dump ??

The component for MENGE seems to be described as a P7 with 2 decimals. And the resultset wanna use a QUAN type... or something like that !

5 REPLIES 5
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,038

Hello

I have expanded your sample coding for selecting three fields out of EKPO:


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_SQL_RESULT_SET
*&
*&---------------------------------------------------------------------*
*& Thread: Dynamic Internal Table creation and population
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1375510"></a>
*&
*& NOTE: Coding for dynamic structure / itab creation taken from:
*& Creating Flat and Complex Internal Tables Dynamically using RTTI
*& <a href="https://wiki.sdn.sap.com/wiki/display/Snippets/Creating+Flat+and+" TARGET="test_blank">https://wiki.sdn.sap.com/wiki/display/Snippets/Creating+Flat+and+</a>
*& Complex+Internal+Tables+Dynamically+using+RTTI
*&---------------------------------------------------------------------*

REPORT  zus_sdn_sql_result_set.

TYPE-POOLS: abap.

DATA:
 go_sql_stmt       TYPE REF TO cl_sql_statement,
 go_resultset      TYPE REF TO cl_sql_result_set,
 gd_sql_clause     TYPE string.

DATA:
  gd_tabfield      TYPE string,
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr_new    TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_handle       TYPE REF TO data,
  gdo_record       TYPE REF TO data,
  gs_comp          TYPE abap_componentdescr,
  gt_components    TYPE abap_component_tab.

FIELD-SYMBOLS:
  <gs_record>   TYPE ANY,
  <gt_itab>     TYPE STANDARD TABLE.

START-OF-SELECTION.

continued.

Read only

0 Likes
1,037

continued.

START-OF-SELECTION.

  DO 3 TIMES.
    CASE syst-index.
      WHEN '1'. gd_tabfield = 'EKPO-EBELN'.
      WHEN '2'. gd_tabfield = 'EKPO-EBELP'.
      WHEN '3'. gd_tabfield = 'EKPO-MENGE'.
    ENDCASE.

    CLEAR: gs_comp.
    gs_comp-type ?= cl_abap_datadescr=>describe_by_name( gd_tabfield ).
    gs_comp-name  = gd_tabfield+5.  " EKPO-EBELN => EBELN
    APPEND gs_comp TO gt_components.
  ENDDO.

  "   Create instances of dynamic structure and dynamic internal table
  go_sdescr_new  = cl_abap_structdescr=>create( gt_components ).
  go_tdescr      = cl_abap_tabledescr=>create( go_sdescr_new ).
  "   Create data refence followed by table creation
  CREATE DATA gdo_record TYPE HANDLE go_sdescr_new.
  CREATE DATA gdo_handle TYPE HANDLE go_tdescr.  " !!!


  CREATE OBJECT go_sql_stmt.
  gd_sql_clause = 'SELECT EBELN, EBELP, MENGE FROM EKPO'.

* Execute the query
  go_resultset = go_sql_stmt->execute_query( gd_sql_clause ).

* Then structure for the Resultset is set
  CALL METHOD go_resultset->set_param_struct( struct_ref = gdo_record ).

* Fetching the lines of the resultset
  TRY.
      WHILE go_resultset->next( ) > 0.
        ASSIGN gdo_record->* TO <gs_record>.
        APPEND <gs_record> TO <gt_itab>.
      ENDWHILE.
    CATCH cx_sql_exception .
    CATCH cx_parameter_invalid_type .
  ENDTRY.
  go_resultset->close( ).

  CALL METHOD cl_salv_table=>factory
    IMPORTING
      r_salv_table = go_table
    CHANGING
      t_table      = <gt_itab>.
  go_table->display( ).

END-OF-SELECTION.


Regards
  Uwe
Read only

0 Likes
1,037

Thanks a lot Uwe, you made it crystal clear!

I had most of the elements, and some mistake (my components where created with element, not data), and your post has highlighted all those issues perfectly.

Regards,

N.H

Read only

0 Likes
1,037

Hello

I think the problem with your coding was that if the result set consists of a single field then you need to take a field (instead of a structure) as returning "structure" for the result set instance.

Regards

Uwe

Read only

0 Likes
1,037

Actually, I was using cl_abap_elemdescr to get my component type, instead of cl_abap_datadescr. This syntax seems to work and no error are found in the code. But at runtime, type is incomplete and a short dump occurs.

Another point (not stated in my 1st post) was that I was creating myself the field catalog for the ALV, but the cl_salv_table you've provided me with, does that better than me !