Application Development 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: 

Move corresponding fields of dynamic table / exclude an internal table of an internal table

Former Member
0 Kudos

Hey experts,

I have the following code, at the bolded line is an error.


FORM update_table .

   DATA: dy_table TYPE REF TO data,

         dy_line TYPE REF TO data.

   FIELD-SYMBOLS: <it_modified> TYPE STANDARD TABLE,

                  <wa_modified>.

   CREATE DATA dy_table TYPE table of (if_table_name).

   ASSIGN dy_table->* TO <it_modified>.

   CREATE DATA dy_line like line of <it_modified>.

   ASSIGN dy_line->* TO <wa_modified>.

   CALL METHOD r_grid->get_selected_rows

     IMPORTING

       et_index_rows = it_selected_rows.

   LOOP AT it_selected_rows INTO wa_selected_rows.

     READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

     IF sy-subrc EQ 0.

       APPEND <wa_modified> TO <it_modified>.

     ENDIF.

   ENDLOOP.

   MODIFY (if_table_name) FROM TABLE <it_modified>.

ENDFORM.                    " UPDATE_TABLE

My problem is, that I want to modify a dtab (if_table_name), but my dynamic internal table <table> is the type of the dtab but populated with a field_style internal table so they are not the same type so I can't simple use the MODIFY statement.

At the code above there is an error on the line

READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

because <table> is populated and <wa_modified> is the type of the dtab. So I would need to read only the corresponding fields of the <table>, but the READ TABLE can't use the CORRESPONDING FIELDS OF statement. Is it somehow possible to solve this problem?


I tried to change the type of the <wa_modified> to the type of the <table>, but then an other error occurs on the following line

MODIFY (if_table_name) FROM TABLE <it_modified>.

because now the <it_modified> has a different type like the dtab (if_table_name), and here I also can't use the corresponding fields of statement.
Do you have an idea for this problem?

I also tried this modification:


DATA: dy_table TYPE REF TO data,

         dy_line TYPE REF TO data,

         dy_line1 type REF TO data.

   FIELD-SYMBOLS: <it_dtab_modified> TYPE STANDARD TABLE,

                  <wa_dtab_modified>,

                  <wa_modified>.

   CREATE DATA dy_table TYPE table of (if_table_name).

   ASSIGN dy_table->* TO <it_dtab_modified>.

   create data dy_line1 like line of <it_dtab_modified>.

   ASSIGN dy_line1 to <wa_dtab_modified>.

   CREATE DATA dy_line type handle ob_abap_structdescr.

   ASSIGN dy_line->* TO <wa_modified>.

   CALL METHOD r_grid->get_selected_rows

     IMPORTING

       et_index_rows = it_selected_rows.

   LOOP AT it_selected_rows INTO wa_selected_rows.

     READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

     IF sy-subrc EQ 0.

       MOVE-CORRESPONDING <wa_modified> to <wa_dtab_modified>.

       APPEND <wa_dtab_modified> TO <it_dtab_modified>.

     ENDIF.

   ENDLOOP.

   MODIFY (if_table_name) FROM TABLE <it_dtab_modified>.

   endform.

but my dynamic field symbols has no structure so I can't use the move-corresponding statement. What should I do so?
Please help me. Thank you for any posts.

Regards,

Robert

1 ACCEPTED SOLUTION

Former Member
0 Kudos

,.Hi Robert,

1. First get list fields using Runtime type services (RTTS) of dtab.

2,  Read table <table> with type compatible workarea

3.  Ater sy-subrc successful check, loop the the list of fields obtained from RTTS, inside loop use     Assign component <field name> of structure 'workarea' to <fs_any>

This is rough idea you have to play with this RTTS to get desire result. I'm not in front of system so only able to give hint

Thanks & Regards,

Arun

9 REPLIES 9

0 Kudos

Hi Robert,

Please find the below code. i think it will helps you..

************************************************************************

*             DECLARATION FOR DYNAMIC TABLE CREATION                   *

************************************************************************

DATA it_fieldcat TYPE lvc_t_fcat WITH HEADER LINE.

FIELD-SYMBOLS : <field>   TYPE any,

                <ls_fcat> TYPE lvc_s_fcat.

DATA : new_table  TYPE REF TO data,

       new_line   TYPE REF TO data,

       new_table1 TYPE REF TO data,

       new_line1  TYPE REF TO data.

***type any table

FIELD-SYMBOLS: <new_tab> TYPE STANDARD TABLE,

              <new_line> TYPE any.

***type any table

FIELD-SYMBOLS: <new_tab1> TYPE STANDARD TABLE,

               <new_line1> TYPE any.

****************************************************************************

   ***For Dynamic Fieldcatalog
  LOOP AT it_rt2.
    it_fieldcat-fieldname = it_rt2-lgtxt.
    it_fieldcat-ref_field = 'LGTXT'.
    it_fieldcat-ref_table = 'ZPT002'.
    it_fieldcat-tabname = 'IT_RT2'.
    APPEND it_fieldcat.
    CLEAR it_fieldcat.
  ENDLOOP.

*****************************************************************************

   ***DYNAMIC TABLE CREATION
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_fieldcat[]
    IMPORTING
      ep_table        = new_table.

  ASSIGN new_table->* TO <new_tab>.
  CREATE DATA new_line LIKE LINE OF <new_tab>.
  ASSIGN new_line->* TO <new_line>.

*****************************************************

   LOOP AT <new_tab> INTO <new_line>.
      MOVE-CORRESPONDING <new_line> TO <new_line1>.
      INSERT <new_line1> INTO TABLE <new_tab1>.
      CLEAR <new_line1>.

    ENDLOOP.

****************************************************************************

Regards,

Sujatha sahu

0 Kudos

I solved it with RTTS in the bellow posts. But yeh it could be done with the fieldcatalog and CALL METHOD cl_alv_table_create=>create_dynamic_table also, so it is a helpful post, thanks.

Former Member
0 Kudos

,.Hi Robert,

1. First get list fields using Runtime type services (RTTS) of dtab.

2,  Read table <table> with type compatible workarea

3.  Ater sy-subrc successful check, loop the the list of fields obtained from RTTS, inside loop use     Assign component <field name> of structure 'workarea' to <fs_any>

This is rough idea you have to play with this RTTS to get desire result. I'm not in front of system so only able to give hint

Thanks & Regards,

Arun

0 Kudos

I created the <table> with RTTS so I know what are you talking about, and yeh it can be a solution but as you said it is a rough idea, I would loop all the fields at every row of the itab, but if nothing else will be there, I will use this idea. Thank you.

0 Kudos

I got an idea from you my problem is, that when I'm using move-corresponding, the <wa_dtab_modified> has no structure because it is created followingly


CREATE DATA dy_table TYPE table of (if_table_name).

   ASSIGN dy_table->* TO <it_dtab_modified>.

   create data dy_line1 like line of <it_dtab_modified>.

ASSIGN dy_line1 to <wa_dtab_modified>.

so all I have to do is to create <wa_dtab_modified> differently and I think the best way is the RTTS where I define the structure or with the CALL METHOD cl_alv_table_create=>create_dynamic_table where I define the fieldcatalog which define the structure. So it should work, but I have to try it out
Then I'll inform you about the results.

0 Kudos

It's working the whole code for others:


FORM update_table .

   DATA: dy_table TYPE REF TO data,

            dy_line TYPE REF TO data,

            dy_line1 TYPE REF TO data,

         dtab_structdescr TYPE REF TO cl_abap_structdescr,

         dtab_tabledescr  TYPE REF TO cl_abap_tabledescr.

   FIELD-SYMBOLS: <it_dtab_modified> TYPE STANDARD TABLE,

                  <wa_dtab_modified>,

                  <wa_modified>.

   PERFORM dtab_rtts CHANGING dtab_structdescr dtab_tabledescr.

   CREATE DATA dy_table TYPE handle dtab_tabledescr.

   ASSIGN dy_table->* TO <it_dtab_modified>.

   CREATE DATA dy_line1 type handle dtab_structdescr.

   ASSIGN dy_line1->* TO <wa_dtab_modified>.

   CREATE DATA dy_line TYPE HANDLE ob_abap_structdescr.

   ASSIGN dy_line->* TO <wa_modified>.

   CALL METHOD r_grid->get_selected_rows

     IMPORTING

       et_index_rows = it_selected_rows.

   LOOP AT it_selected_rows INTO wa_selected_rows.

     READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

     IF sy-subrc EQ 0.

       MOVE-CORRESPONDING <wa_modified> TO <wa_dtab_modified>.

       APPEND <wa_dtab_modified> TO <it_dtab_modified>.

     ENDIF.

   ENDLOOP.

   MODIFY (if_table_name) FROM TABLE <it_dtab_modified>.


endform.



And the dtab_rtts form



FORM dtab_rtts  CHANGING p_dtab_structdescr

                          p_dtab_tabledescr.

   DATA: it_component TYPE cl_abap_structdescr=>component_table .

   DATA: st_component LIKE LINE OF it_component,

         it_fields TYPE TABLE OF dfies,

         wa_fields TYPE dfies.

   CALL FUNCTION 'DDIF_FIELDINFO_GET'

     EXPORTING

       tabname        = if_table_name

     TABLES

       dfies_tab      = it_fields

     EXCEPTIONS

       not_found      = 1

       internal_error = 2

       OTHERS         = 3.

   LOOP AT it_fields INTO wa_fields.

     st_component-name = wa_fields-fieldname.

     st_component-type ?= cl_abap_elemdescr=>describe_by_name( wa_fields-rollname ).

     APPEND st_component TO it_component .

   ENDLOOP.

    TRY.

       p_dtab_structdescr = cl_abap_structdescr=>create( it_component ).

     CATCH cx_sy_struct_creation .

   ENDTRY.

   TRY.

       p_dtab_tabledescr = cl_abap_tabledescr=>create( p_dtab_structdescr ).

     CATCH cx_sy_table_creation .

   ENDTRY.

ENDFORM.                    " DTAB_RTTS

former_member192854
Active Participant
0 Kudos

Hi Robert,

when I look to this code;

   LOOP AT it_selected_rows INTO wa_selected_rows.

     READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

     IF sy-subrc EQ 0.

       APPEND <wa_modified> TO <it_modified>.

     ENDIF.

   ENDLOOP.


You can consider to assign the read table in a dynamic field symbol:

READ TABLE <table> ASSIGNING <fs_t> INDEX wa_selected_rows-index.


Then create a default structure which matches it_modified, or create one via a data reference:

CREATE DATA wa_modified LIKE LINE OF it_modified.

ASSIGN wa_modified->* TO <wa_modified>.

Then move the data:

MOVE CORRESPONDING FIELDS OF <fs_t> TO <wa_modified>.

And then APPEND (or INSERT as I should prefer):

INSERT <wa_modified> INTO TABLE <it_modified>.


Best,

Sander

0 Kudos

I'm not sure, but it sounds like my second code which I posted here


DATA: dy_table TYPE REF TO data,

         dy_line TYPE REF TO data,

         dy_line1 type REF TO data.

   FIELD-SYMBOLS: <it_dtab_modified> TYPE STANDARD TABLE,

                  <wa_dtab_modified>,

                  <wa_modified>.

   CREATE DATA dy_table TYPE table of (if_table_name).

   ASSIGN dy_table->* TO <it_dtab_modified>.

   create data dy_line1 like line of <it_dtab_modified>.

   ASSIGN dy_line1 to <wa_dtab_modified>.

   CREATE DATA dy_line type handle ob_abap_structdescr.

   ASSIGN dy_line->* TO <wa_modified>.

   CALL METHOD r_grid->get_selected_rows

     IMPORTING

       et_index_rows = it_selected_rows.

   LOOP AT it_selected_rows INTO wa_selected_rows.

     READ TABLE  <table> INTO  <wa_modified> INDEX wa_selected_rows-index.

     IF sy-subrc EQ 0.

       MOVE-CORRESPONDING <wa_modified> to <wa_dtab_modified>.

       APPEND <wa_dtab_modified> TO <it_dtab_modified>.

     ENDIF.

   ENDLOOP.

   MODIFY (if_table_name) FROM TABLE <it_dtab_modified>.

   endform.

and it doesn't work because the wa_dtab_modified is created dynamically and it says it has no structure or deep structure I don't remember currently.
I can't try it out now, but I will go tru your post once again and try it out. Thank you.

0 Kudos

good luck!