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

Insert data into dynamic internal table with some conditions

Former Member
0 Likes
587

Hi,

i have all complete, but have a small (big) problem with filling data into dynamic table, i have this code:

i have <fs_dyn_tab> with dynamic specified fields, and have in 5 filled fields with data... i need to dynamically insert values to columns, that are specified by user in sm30 table (thats table with routine table-fieldnames that i must filled in dynamic internal table)

for example: the user adds to sm30 table MVKE-MTPOS and i have only VKORG and VTWEG in my code, how i can add values to this column? i have this data in lt_mvke, but i dont know how to fill dyn. table with it without coding condition for MAKT-MTPOS. Is it problem, because the number of columns may be obvious.....

p.s. the empty cells must be filled by 'space'

thx for your replies friends.

  IF ch_mvke EQ 'X'.

    SELECT * FROM mvke
      INTO CORRESPONDING FIELDS OF TABLE lt_mvke
      WHERE vkorg IN s_vkorg
      AND vtweg IN s_vtweg
      AND matnr IN s_matnr.

    DESCRIBE TABLE <fs_dyn_tab> LINES lv_lines.

    LOOP AT <fs_dyn_tab> ASSIGNING <fs_dyn_first>.

      lv_count = lv_count + 1.

      ASSIGN COMPONENT 'MATNR'
      OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

      IF sy-subrc EQ 0.
        IF lt_mvke IS INITIAL.

          IF s_vkorg IS INITIAL.

            ASSIGN COMPONENT 'VKORG'
                    OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

            IF sy-subrc EQ 0.
              CLEAR <fs_fldval>.
            ENDIF.

            ASSIGN COMPONENT 'VTWEG'
                    OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

            IF sy-subrc EQ 0.
              CLEAR <fs_fldval>.
            ENDIF.

          ELSE.

            lv_exist = 0.

          ENDIF.

        ELSE.

          CLEAR lv_add_line.
          lv_contain = 1.

          LOOP AT lt_mvke REFERENCE INTO lr_mvke
            WHERE matnr EQ <fs_fldval>.

            IF lv_add_line EQ 0.
              IF lr_mvke->vkorg IS NOT INITIAL.

                ASSIGN COMPONENT 'VKORG'
                  OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

                IF sy-subrc EQ 0.
                  <fs_fldval> = lr_mvke->vkorg.
                  IF NOT <fs_fldval> IN s_vkorg.
                    lv_exist = 0.
                    EXIT.
                  ELSE.
                    lv_exist = 1.
                  ENDIF.
                ENDIF.
              ENDIF.

              IF lr_mvke->vtweg IS NOT INITIAL.
                ASSIGN COMPONENT 'VTWEG'
                  OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

                IF sy-subrc EQ 0.
                  <fs_fldval> = lr_mvke->vtweg.
                  IF NOT <fs_fldval> IN s_vtweg.
                    lv_exist = 0.
                    EXIT.
                  ELSE.
                    lv_exist = 1.
                  ENDIF.
                ENDIF.
              ENDIF.

              lv_add_line = 1.

            ELSE.

              <fs_dyn_rest> = <fs_dyn_first>.

              IF lr_mvke->vkorg IS NOT INITIAL.
                ASSIGN COMPONENT 'VKORG'
                  OF STRUCTURE <fs_dyn_rest> TO <fs_fldval>.

                IF sy-subrc EQ 0.
                  <fs_fldval> = lr_mvke->vkorg.
                  IF NOT <fs_fldval> IN s_vkorg.
                    lv_exist = 0.
                    EXIT.
                  ELSE.
                    lv_exist = 1.
                  ENDIF.
                ENDIF.
              ENDIF.

              IF lr_mvke->vtweg IS NOT INITIAL.
                ASSIGN COMPONENT 'VTWEG'
                  OF STRUCTURE <fs_dyn_rest> TO <fs_fldval>.

                IF sy-subrc EQ 0.
                  <fs_fldval> = lr_mvke->vtweg.

                  IF NOT <fs_fldval> IN s_vtweg.
                    lv_exist = 0.
                    EXIT.
                  ELSE.
                    lv_exist = 1.
                  ENDIF.
                ENDIF.
              ENDIF.

              APPEND <fs_dyn_rest> TO <fs_dyn_tab>.

            ENDIF.

            lv_contain = 0.

          ENDLOOP.


          IF lv_contain EQ 1.
            IF s_vkorg IS INITIAL.

              ASSIGN COMPONENT 'VKORG'
                OF STRUCTURE <fs_dyn_first> TO <fs_fldval>.

              IF sy-subrc EQ 0.
                CLEAR <fs_fldval>.
              ENDIF.

            ELSE.
              lv_exist = 0.
            ENDIF.
          ENDIF.

        ENDIF.

      ENDIF.

      IF lv_exist EQ 0.
        DELETE <fs_dyn_tab> INDEX sy-tabix.
      ENDIF.

      IF lv_count EQ lv_lines.
        lv_exist = 1.
        lv_contain = 1.
        EXIT.
      ENDIF.

    ENDLOOP.

    SORT <fs_dyn_tab> BY ('MATNR') ('VKORG').

    CLEAR lv_count.

  ENDIF.

<Added code tags>

Message was edited by: Suhas Saha

3 REPLIES 3
Read only

Clemenss
Active Contributor
0 Likes
519

Hi michal suchek,

I do not fully understand your requirement and I cant't follow your code

At least I learned something new "REFERENCE INTO lr_mvke" requires lr_mvke to be of TYPE REF TO  MVKE. This means lr_mvke is an object with public attributes with the names of the components of structure MVKE. Thanks for the enlightenment - never used like that.

Anyway, may I simplify?

  DATA:

    lt_mvke             TYPE TABLE OF mvke.

  FIELD-SYMBOLS:

    <fs_dyn_tab>        TYPE table,

    <mvke>              TYPE ANY,

    <line>              TYPE ANY,

    <any>               TYPE ANY,

    <matnr>             TYPE ANY,

    <vkorg>             TYPE ANY,

    <vtweg>             TYPE ANY.

  LOOP AT <fs_dyn_tab> ASSIGNING <any>.

    ASSIGN COMPONENT 'MATNR' OF STRUCTURE <any> TO <matnr>.

    CHECK sy-subrc = 0.

    ASSIGN COMPONENT 'VKORG' OF STRUCTURE <any> TO <vkorg>.

    CHECK sy-subrc = 0.

    ASSIGN COMPONENT 'VTWEG' OF STRUCTURE <any> TO <vtweg>.

    CHECK sy-subrc = 0.

    READ TABLE lt_mvke ASSIGNING <mvke>

      WITH KEY

      matnr = <matnr>

      vkorg = <vkorg>

      vtweg = <vtweg>

      BINARY SEARCH.

    MOVE-CORRESPONDING <mvke> TO <any>.

  ENDLOOP.

Start like this. Simplification rules 🙂

Regards

Clemens

Read only

Former Member
0 Likes
519

hi,

thx for your reply. I am new in abap programming, thx for tips i would like to try this

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
519

Hello Former Member & ,

   DATA: gt_flight TYPE SORTED TABLE OF sflight WITH NON-UNIQUE KEY carrid,
*      gr_flight TYPE REF TO data,

       gr_flight TYPE REF TO sflight.

FIELD-SYMBOLS: <gs_flight> TYPE ANY.

SELECT * FROM sflight INTO TABLE gt_flight UP TO 20 ROWS.
IF sy-subrc = 0.
  LOOP AT gt_flight REFERENCE INTO gr_flight.
    CHECK gr_flight->carrid IS NOT INITIAL.
  ENDLOOP.
ENDIF.

This code doesn't syntax check! Although I can dereference the data reference & process

Can you please tell me what i'm doing wrong?

BR,

Suhas

PS: Sorry my bad! The data reference should be fully-typed to make the above code working



Message was edited by: Suhas Saha