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

CONVERT ROWS INTO COLUMNS IN INTERNAL TABLE

Former Member
0 Likes
10,586

Hi Experts,

I want to convert rows into coloumns in final internal table.

How to do that one. Can any one help me its very urgent.

Regards,

PBS.

1 ACCEPTED SOLUTION
Read only

Former Member
3,561

Check this example, is more dynamic.


TYPES: BEGIN OF ty_1,
first,
second,
third,
END OF ty_1.

TYPES: BEGIN OF ty_2,
first,
second,
third,
END OF ty_2.

FIELD-SYMBOLS: <fs> TYPE ANY,
              <fs2> TYPE ANY.

DATA: itab1 TYPE TABLE OF ty_1 WITH HEADER LINE,
      itab2 TYPE TABLE OF ty_1 WITH HEADER LINE,
      curr_line TYPE sy-tabix.

itab1-first = 'a'.
itab1-second = 'a'.
itab1-third = 'a'.
APPEND itab1.

itab1-first = 'b'.
itab1-second = 'b'.
itab1-third = 'b'.
APPEND itab1.

itab1-first = 'c'.
itab1-second = 'c'.
itab1-third = 'c'.
APPEND itab1.

LOOP AT itab1.
  CLEAR itab2.
  ASSIGN COMPONENT sy-tabix OF STRUCTURE itab2 TO <fs>.
  curr_line = sy-tabix.
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE itab1 TO <fs2>.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    IF curr_line = 1.
      <fs> = <fs2>.
      APPEND itab2.
    ELSE.
      READ TABLE itab2 INDEX sy-index.
      <fs> = <fs2>.
      MODIFY itab2 INDEX sy-index.
    ENDIF.
  ENDDO.
ENDLOOP.

Hi Experts,

I want to convert rows into coloumns in final internal table.

How to do that one. Can any one help me its very urgent.

Regards,

PBS.

2 REPLIES 2
Read only

Former Member
0 Likes
3,561

hi,

Find the below code for changing rows into colums.

data: begin of itab1 occurs 0,

fld,

end of itab1.

data: begin of itab2 occurs 0,

fld1,

fld2,

fld3,

end of itab2.

itab1-fld = 1.

append itab1.

itab1-fld = 2.

append itab1.

itab1-fld = 3.

append itab1.

read table itab1 index 1.

if sy-subrc eq 0.

itab2-fld1 = itab1-fld.

endif.

read table itab1 index 2.

if sy-subrc eq 0.

itab2-fld2 = itab1-fld.

endif.

read table itab1 index 3.

if sy-subrc eq 0.

itab2-fld3 = itab1-fld.

endif.

append itab2.

loop at itab1.

write:/ itab1.

endloop.

loop at itab2.

write:/ itab2.

endloop.

refer the below link for further information

Read only

Former Member
3,562

Check this example, is more dynamic.


TYPES: BEGIN OF ty_1,
first,
second,
third,
END OF ty_1.

TYPES: BEGIN OF ty_2,
first,
second,
third,
END OF ty_2.

FIELD-SYMBOLS: <fs> TYPE ANY,
              <fs2> TYPE ANY.

DATA: itab1 TYPE TABLE OF ty_1 WITH HEADER LINE,
      itab2 TYPE TABLE OF ty_1 WITH HEADER LINE,
      curr_line TYPE sy-tabix.

itab1-first = 'a'.
itab1-second = 'a'.
itab1-third = 'a'.
APPEND itab1.

itab1-first = 'b'.
itab1-second = 'b'.
itab1-third = 'b'.
APPEND itab1.

itab1-first = 'c'.
itab1-second = 'c'.
itab1-third = 'c'.
APPEND itab1.

LOOP AT itab1.
  CLEAR itab2.
  ASSIGN COMPONENT sy-tabix OF STRUCTURE itab2 TO <fs>.
  curr_line = sy-tabix.
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE itab1 TO <fs2>.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    IF curr_line = 1.
      <fs> = <fs2>.
      APPEND itab2.
    ELSE.
      READ TABLE itab2 INDEX sy-index.
      <fs> = <fs2>.
      MODIFY itab2 INDEX sy-index.
    ENDIF.
  ENDDO.
ENDLOOP.