Skip to Content
avatar image
Former Member

Assign data to internal table that have same name with the data.

Dear All,

I have a problem.I must create a report for internal auditor in my office.

I have two internal table , first internal table it has 2 field ( user and role )

The example data is like this :

user role

aap2 role1

aap2 role2

als role1

asu role1

emu role1

emu role3

ysu role4

I want to map my data in first internal table to second internal table, so the data in second internal table will be like this :

Second internal table has 5 fields ( user, role1, role2, role3,role4)

user aap2 have mark 'X' in field role1 and role2.

user als have mark 'X' in field role1.

user asu have mark 'X' in field role1.

user emu have mark 'X' in field role1 and role3.

user ysu have mark 'X' in field role4.

How to move data the data without using if and else?

And the other concern is number of field in second internal table is automatically increase depend on field role in the first intrenal table.

If the data in field role in first internal table is role1, role2, second internal table has 3 fields ( user, role1, role2).

if the data in field role is role1, role2, role3, second internal table has 4 fields ( user, role1, role2, role3 ).

etc )

I very need your help.

Thanks.

Best Regard,

Kuskus.

Edited by: Kuskus on Sep 6, 2010 4:54 AM

Edited by: Kuskus on Sep 6, 2010 4:57 AM

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Sep 06, 2010 at 04:45 AM

    Hi,

    Try the below code it will be helpful.

    Declare a variable of role

    
     DATA: role(5) TYPE c.   "Declare a variable role 
    
    DATA: BEGIN OF fs_int1,
      user(10) TYPE c,
      role(5) TYPE c,
      END OF fs_int1.
    
    DATA: BEGIN OF fs_int2,
      user(10) TYPE c,
      role1 TYPE c,
      role2 TYPE c,
      role3 TYPE c,
      role4 TYPE c,
      END OF fs_int2.
    
    DATA: t_int1 LIKE STANDARD TABLE OF fs_int1,
           t_int2 LIKE STANDARD TABLE OF fs_int2.
    
    fs_int1-user = 'aap2'.
    fs_int1-role = 'role1'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'aap2'.
    fs_int1-role = 'role2'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'als'.
    fs_int1-role = 'role1'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'asu'.
    fs_int1-role = 'role1'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'emu'.
    fs_int1-role = 'role1'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'emu'.
    fs_int1-role = 'role3'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    fs_int1-user = 'ysu'.
    fs_int1-role = 'role4'.
    APPEND fs_int1 TO t_int1.
    CLEAR fs_int1.
    
    "While using loop with second internal table loop as follows.
    
    LOOP AT t_int1 INTO fs_int1.              
      MOVE fs_int1-user TO fs_int2-user.
      MOVE fs_int1-role TO role.
      CASE role.
        WHEN 'role1'.
          MOVE 'X' TO fs_int2-role1.
        WHEN 'role2'.
          MOVE 'X' TO fs_int2-role2.
        WHEN 'role3'.
          MOVE 'X' TO fs_int2-role3.
        WHEN 'role4'.
          MOVE 'X' TO fs_int2-role4.
    
      ENDCASE.
      APPEND fs_int2 TO t_int2.
      CLEAR fs_int2.
      CLEAR role.
    
    ENDLOOP.
    
    write: 'USER',11 'ROLE1',19 'ROLE2',27 'ROLE3',38 'ROLE4'.
    
    LOOP AT t_int2 INTO fs_int2.
      WRITE:/ fs_int2-user,
           11 fs_int2-role1,
           19 fs_int2-role2,
            27 fs_int2-role3,
            38 fs_int2-role4.
    ENDLOOP.
    
    

    Hope it is helpful

    Regards,

    Phani Kumar M

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 06, 2010 at 06:23 AM

    Check out the below wiki for adding dynamic fields to your second internal table.....

    http://wiki.sdn.sap.com/wiki/display/Snippets/Addacolumntoaninternaltable+dynamically

    Regards,

    Lalit Mohan Gupta

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I'am succesfully add column dinamically in my internal table by using method CALL METHOD cl_alv_table_create=>create_dynamic_table. Thanks for your help.

      But I have another problem, how to move automatically my data in first internal table to second internal table based on data in my first internal table?

      The example is like this :

      itab1 (first internal table) have field user.

      itab2 (second internal table) have a field emu, ysu, asu.

      I want my program to be like this :

      if itab1-user = 'EMU'.

      itab2-emu = 'X'.

      itab2-ysu = ' '.

      itab2-asu = ' '.

      elseif itab1-user = 'YSU'.

      itab2-emu = ' '.

      itab2-ysu = 'X'.

      itab2-asu = ' '.

      elseif itab1-user = 'ASU'.

      itab2-emu = ' '.

      itab2-ysu = ' '.

      itab2-asu = 'X'.

      endif.

      What I mean automatically is I don't want to use If...end if , what I want is if itab1-user is EMU, so the field in itab2 with name EMU will be filled by 'X'.

      Is there any way to do this?

      Thank you so much.

      Best Regard,

      Kuskus.

      Edited by: Kuskus on Sep 21, 2010 1:13 PM

      Edited by: Kuskus on Sep 21, 2010 1:13 PM

  • avatar image
    Former Member
    Sep 21, 2010 at 11:18 AM

    Hello Kuskus,

    Following will be the code for your requirement

    field-symbols: <fs> type (line-type of itab2) (<fs> type mara),

    <fs1>.

    sort itab1 by col1.

    loop at itab1.

    assign component (itab1-col2) of structure <fs> to <fs1>.

    <fs1> = 'X'.

    unassign <fs1>.

    at end of col1.

    append <fs> to itab2.

    endat.

    endloop.

    Hope this helps,

    Regards,

    Himanshu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 21, 2010 at 11:19 AM

    Hello Kuskus,

    Following will be the code for your requirement

    field-symbols: <fs> type (line-type of itab2) (<fs> type mara),

    <fs1>.

    sort itab1 by col1.

    loop at itab1.

    assign component (itab1-col2) of structure <fs> to <fs1>.

    <fs1> = 'X'.

    unassign <fs1>.

    at end of col1.

    append <fs> to itab2.

    endat.

    endloop.

    Hope this helps,

    Regards,

    Himanshu

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      hello Himanshu...your answer is very helpful.

      I tried it in my program like this :

      REPORT zkus_auth4.

      DATA : BEGIN OF itab1 OCCURS 1,

      bname LIKE ust04-bname,

      profile LIKE ust04-profile,

      END OF itab1.

      DATA : BEGIN OF itab2 OCCURS 1,

      aap2 TYPE c,

      asu TYPE c,

      emu TYPE c,

      END OF itab2.

      START-OF-SELECTION.

      • Fill itab1*

      MOVE 'AAP2' TO itab1-bname.

      MOVE 'PROFILE1' TO itab1-profile.

      APPEND itab1.

      MOVE 'AAP2' TO itab1-bname.

      MOVE 'PROFILE2' TO itab1-profile.

      APPEND itab1.

      MOVE 'ASU' TO itab1-bname.

      MOVE 'PROFILE1' TO itab1-profile.

      APPEND itab1.

      MOVE 'EMU' TO itab1-bname.

      MOVE 'PROFILE3' TO itab1-profile.

      APPEND itab1.

      FIELD-SYMBOLS: <fs> like line of itab1,

      <fs1>.

      SORT itab1 BY bname.

      LOOP AT itab1.

      ASsIGN COMPONENT itab1-bname OF STRUCTURE <fs> TO <fs1>.

      <fs1> = 'X'.

      UNASSIGN <fs1>.

      AT END OF bname.

      APPEND <fs> TO itab2.

      ENDAT.

      ENDLOOP.

      But when I execute the program, it show a short dump with title like this :

      MOVE_TO_LIT_NOTALLOWED_NODATA

      with the explanation :

      Assignment error : Overwriting a protected field.

      Error analysis : The program tried to assign a new value to field even though it is protected against change.

      Is the any wrong my code?

      I have read all of the error analysis but I still did not get the answer.

      Please help me.

      Thank you very much.

      Edited by: Kuskus on Sep 22, 2010 6:18 AM