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

How to create a normal internal table dynamically from a dynamic itable?

Former Member
0 Likes
3,230

Hello Folks,

I have a requirement for which i need to create a normal internal table ( of type standard table ) dynamically from a dynamic internal table ( created VIA cl_alv_table_create=>create_dynamic_table).

I will appreciate your response.

12 REPLIES 12
Read only

Shahid
Product and Topic Expert
Product and Topic Expert
0 Likes
2,030

normal internal table is same as dynamic internal table. i believe both can be used in the same way.

you might be using some structure to create the table dynamically. with the same structure you can create an internal table...

or i have had completely mis-understood your question

Read only

SureshRa
Active Participant
0 Likes
2,030

Hi Charan,

Use RTTS (Run Time Type Services) to dynamically create an internal table type.

With your input "Dynamic Internal Table" identify the type using RTTI (Run Time Type Identification) and create target internal table through RTTC (Run Time Type Creation).

Refer to the class CL_ABAP_TABLEDESCR. Also refer to this link [http://help-abap.blogspot.com/2008/09/dynamic-internal-table-creation.html|http://help-abap.blogspot.com/2008/09/dynamic-internal-table-creation.html]

Regards

Suresh

Read only

SureshRa
Active Participant
0 Likes
2,030

Another alternative approach:


FIELD-SYMBOLS <fs_table> TYPE ANY TABLE.
DATA dref TYPE REF TO DATA.

*Assuming your dynamic source internal table is DYN_TAB.
CREATE DATA dref LIKE STANDARD TABLE OF dyn_tab WITH NON-UNIQUE DEFAULT KEY.
ASSIGN dref->* TO <fs_table>.
*<fs_table> is now your target standard table. 

Regards

Suresh

Read only

Shahid
Product and Topic Expert
Product and Topic Expert
0 Likes
2,030

Not sure whether it will work if we declare "STANDARD TABLE OF some dyn created table"

FIELD-SYMBOLS: <fs_dyn_table> TYPE STANDARD TABLE.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = lt_fcat
    IMPORTING
      ep_table        = lt_dy_table.


  ASSIGN lt_dy_table->* TO <fs_dyn_table>.

  ASSIGN lt_dy_table_new->* TO <fs_dyn_table_new>.

Read only

Clemenss
Active Contributor
0 Likes
2,030

Hi Charan,

except that the use of cl_alv_table_create=>create_dynamic_table is critical, you ca create what ever where ever you want:

field-symbols:
  <table> type any table.
data:
  lr_any type ref to data.
CREATE DATA lr_any LIKE <your dynamic table>>.
ASSIGN lr_any->* to <table>.

Thats is it. <table> is a new internal table with same characteristics as the table it was created like.

Regards

Clemens.

Read only

Former Member
0 Likes
2,030

Hi ,

use the below code to dynamic display the internal table .You need to create container for dynamically display your intenal table.

data: it_fieldcat type lvc_s_fcat occurs 0,

new_table type ref to data,

new_line type ref to data,

data : g_custom_container type ref to cl_gui_custom_container,"Container1

g_handler type ref to lcl_event_handler. "handler

call method cl_alv_table_create=>create_dynamic_table

exporting

  • I_STYLE_TABLE =

it_fieldcatalog = it_fieldcat

  • I_LENGTH_IN_BYTE =

importing

ep_table = new_table

  • E_STYLE_FNAME =

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

assign new_table->* to <dyn_table>.

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

if sy-subrc ne 0.

message e002(sy) with 'Failed to create dynamic table'.

endif.

Edited by: anshucse on Sep 5, 2011 3:50 PM

Read only

0 Likes
2,030

Thank you all for the replies,

I have my fields comming from a custom table for which i created a dynamic internal table structure. Now i want the dynamic internal table structure in an normal standard internal table ( because my logic is already build via normal internal table.

Please help me how to change/convert the dynamic internal table structure( from field-symbol ) to normal standard internal table.

Thanks again in advance!

Read only

0 Likes
2,030

Hi,

I have my fields comming from a custom table for which i created a dynamic internal table structure. Now i want the dynamic internal table structure in an normal standard internal table ( because my logic is already build via normal internal table.

I don't really get it

Why then you are not directly using that normal internal table when retrieving data from your custom table?

Well, otherwise, if structure of both tables differs, I guess you would have to loop on your dynamic table, assign each component one by one to a new field-symbol and update your normal internal table accordingly.

something like:


LOOP at <dyntab> ASSIGNING <dynwa>.
  LOOP AT fcat INTO wa_fcat.    "your field catalog
    ASSIGN COMPONENT wa_fcat-fieldname OF STRUCTURE <dynwa> to <fs1>.
    ASSIGN COMPONENT wa_fcat-fieldname OF STRUCTURE wa_norm_tab to <fs2>.
    IF sy-subrc EQ 0.
      <fs2> = <fs1>.
    ENDIF.
  ENDLOOP.
  APPEND wa_norm_tab TO gt_norm_tab.
ENDLOOP.

If your dynamic table structure matches the structure of your normal internal table, a simple assignment should be ok..


gt_norm_tab[] = <dyntab>[].

Kr,

m.

ENDIF.

Read only

arundec
Product and Topic Expert
Product and Topic Expert
0 Likes
2,030

You cannot 'convert' an internal table created dynamically - either an internal table is defined statically using the DATA statement, or it is created dynamically using the CREATE DATA statement - if you create an internal table this way it is essentially a 'nameless' internal table.

What are you trying to do with the internal table, i.e. why do you think that you need it to be a static itab?

Read only

0 Likes
2,030

I'll try to explain my req simple.. I defined a static structure and displayed the fields as columns in alv tree, ( i have very complex code developed in my requirement using normal internal tables for eg: sorting, grouping, downloading etc) ...

Now my new req is that .. the early defined static structure fields are now maintained in a custom DB table for which i have to build the structures dynamically.

I used the method CALL METHOD cl_alv_table_create=>create_dynamic_table

This will return a dynamic field symbol structure .

I am thinking that instead of changing my entire complex code into the dynamic field symbols .. keeping the complexity in mind i thought of using my same old internal tables. for this i have to find a way how to change the dynamic field symbols structure to normal standard table structure.

Read only

0 Likes
2,030

Maybe I'm getting it wrong too: You have two tables with different types and different lines types (which are both structures with some corresponding fields), and you need to get the data from one to the other?

If this is so, this (slightly modified and not well tested) code might help you:


*  class-methods COPY_STRUCTURED_TABLE
*    importing
*      !IT_SOURCE type ANY TABLE
*    changing
*      !CT_TARGET type ANY TABLE .

METHOD copy_structured_table.
  DATA: table_type_source  TYPE REF TO cl_abap_tabledescr,
        table_type_target  TYPE REF TO cl_abap_tabledescr,
        struct_type_source TYPE REF TO cl_abap_structdescr,
        struct_type_target TYPE REF TO cl_abap_structdescr,
        comp_tab_source    TYPE cl_abap_structdescr=>included_view,
        comp_tab_target    TYPE cl_abap_structdescr=>included_view,
        data               TYPE REF TO data,
        index              TYPE i.

  FIELD-SYMBOLS: <comp> LIKE LINE OF comp_tab_source,
                 <line_source>  TYPE ANY,
                 <line_target>  TYPE ANY,
                 <field_source> TYPE ANY,
                 <field_target> TYPE ANY.

  table_type_source ?= cl_abap_typedescr=>describe_by_data( it_source ).
  table_type_target ?= cl_abap_typedescr=>describe_by_data( ct_target ).

  struct_type_source ?= table_type_source->get_table_line_type( ).
  struct_type_target ?= table_type_target->get_table_line_type( ).

  comp_tab_source = struct_type_source->get_included_view( ).
  comp_tab_target = struct_type_target->get_included_view( ).

  SORT comp_tab_source BY name.

  LOOP AT comp_tab_target ASSIGNING <comp>.
    index = sy-tabix.

    READ TABLE comp_tab_source WITH KEY name = <comp>-name TRANSPORTING NO FIELDS BINARY SEARCH.

    IF ( sy-subrc NE 0 ).
      DELETE comp_tab_target INDEX index.
    ENDIF.
  ENDLOOP.

  CREATE DATA data TYPE HANDLE struct_type_target.

  ASSIGN data->* TO <line_target>.

  LOOP AT it_source ASSIGNING <line_source>.
    CLEAR <line_target>.

    LOOP AT comp_tab_target ASSIGNING <comp>.
      ASSIGN COMPONENT <comp>-name OF STRUCTURE <line_source> TO <field_source>.
      ASSIGN COMPONENT <comp>-name OF STRUCTURE <line_target> TO <field_target>.

      <field_target> = <field_source>.
    ENDLOOP.

    INSERT <line_target> INTO TABLE ct_target.
  ENDLOOP.

ENDMETHOD.

Read only

0 Likes
2,030

You can implement the solution as per Carsten's reply.

But, since your fields are now coming from a DB table, you could have lot of difference in your "defined normal table fields" and your DB tables - DB could have more fields than your "normal" table or less, in a different sequence or a typo.

So, if you want your program to be fully dynamic, you got to change your existing logic which you already have wrote for defined table to Dynamic table. If you don't make your program fully dynamic than it wont be able to keep up with your DB field changes. Moving data from one dynamic table to "normal" table would defeat the purpose of maintaining the fields in the DB.

Regards,

Naimesh Patel