How to move internal tables of rows to columns?

I want to move the inner rows of a table to a column and I want to fix the first column, I am trying to do it with the following code, but I am not getting the expected result .... it does not convert all rows to columns

*Types Declaration
Types: BEGIN OF ty_t001w,
         ekorg TYPE t001w-ekorg,
         werks TYPE t001w-werks,
         name1 TYPE t001w-name1,
       END OF ty_t001w.

**Field Symbols Declaration
 FIELD-SYMBOLS:  <fs1> TYPE any,
                 <fs2> TYPE any.

**Internal table and work area declaration
 DATA: it1_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa1_col_row TYPE ty_t001w,
       it2_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa2_col_row TYPE ty_t001w,
       cline   TYPE sy-tabix.

**Filling internal table with data

Select *
  from t001w into corresponding fields of table it1_col_row
  where ekorg = p_ekorg
  and fabkl = p_fabkl.

**Looping Internal table to display data
 LOOP AT it1_col_row INTO wa1_col_row.
   WRITE: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1.
 ENDLOOP.
 WRITE: /.

**Looping internal table to change rows into columns
 LOOP AT it1_col_row INTO wa1_col_row.
   CLEAR wa2_col_row.
   ASSIGN COMPONENT sy-tabix OF STRUCTURE wa2_col_row TO <fs1>.
   cline = sy-tabix.
   DO.
     ASSIGN COMPONENT sy-index OF STRUCTURE wa1_col_row TO <fs2>.
     IF sy-subrc NE 0.
       EXIT.
     ENDIF.
     IF cline = 1.
       <fs1> = <fs2>.
       APPEND wa2_col_row TO it2_col_row.
     ELSE.
       READ TABLE it2_col_row INTO wa2_col_row INDEX sy-index.
       <fs1> = <fs2>.
       MODIFY it2_col_row FROM wa2_col_row INDEX sy-index.
     ENDIF.
   ENDDO.
 ENDLOOP.
*
**Looping internal table to display
 LOOP AT it2_col_row INTO wa2_col_row.
   WRITE: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1.
 ENDLOOP.

      

+3


source to share


1 answer


Note that your field types ty_t001w

are of different length:

  • ekorg TYPE t001w-ekorg

    It has CHAR 4

  • werks TYPE t001w-werks

    has also CHAR 4

    , but
  • name1 TYPE t001w-name1

    It has CHAR 30

You are using the same type ty_t001w

for your source table ( it1_col_row

) as well as your target table ( it2_col_row

). So when you map the source row table to the target column table then a 30 character field is name1

mapped to a 4 character field ekorg

. When I ran your program on my system, I had the following output (depending on the contents of my DB table t001w

):

0001 0001 Werk 0001
0001 0002 Werk 0002
0001 0003 Werk 0003
0001 RAD1 Werk RAD1

0001 0001 0001
0001 0002 RAD1
Werk Werk Werk RAD1

      

At first glance, it looks like this: "this is not a conversion of all rows to columns." But in the debugger I noticed that "Werk 0001" is actually one , not two! However, the value is only truncated to "Werk" because it maps from a 30-character field to a 4-character field. This happens with the bottom value of columns 1 ("Werk 0002") and 2 ("Werk 0003"). The bottom value for column 3 ("Werk RAD1") is displayed correctly because it is displayed here from a 30 character field to a 30 character field.



To fix this problem, I created an additional TYPES

definition ty_t001w_col

for the target table it2_col_row

. In this, TYPE

all fields have a maximum length of no more than 30 characters, which ensures no truncation (see below abap code). It generates the following output:

0001 0001 Werk 0001
0001 0002 Werk 0002
0001 0003 Werk 0003
0001 RAD1 Werk RAD1

0001                           0001                           0001
0001                           0002                           RAD1
Werk 0001                      Werk 0002                      Werk RAD1

      

Corrected report:

REPORT zhd_stackoverflow_q27163908.

PERFORM function
      USING '0001'
            '01'.

FORM function
  USING p_ekorg TYPE ekorg
        p_fabkl TYPE fabkl.

Types Declaration
TYPES: BEGIN OF ty_t001w,
         ekorg TYPE t001w-ekorg,
         werks TYPE t001w-werks,
         name1 TYPE t001w-name1,
       END OF ty_t001w.
TYPES: BEGIN OF ty_t001w_col,
         ekorg TYPE t001w-name1,
         werks TYPE t001w-name1,
         name1 TYPE t001w-name1,
       END OF ty_t001w_col.

*Field Symbols Declaration
 FIELD-SYMBOLS:  <fs1> TYPE any,
                 <fs2> TYPE any.

*Internal table and work area declaration
 DATA: it1_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa1_col_row TYPE ty_t001w,
       it2_col_row TYPE STANDARD TABLE OF ty_t001w_col,
       wa2_col_row TYPE ty_t001w_col,
       cline   TYPE sy-tabix.

*Filling internal table with data

SELECT *
  FROM t001w INTO CORRESPONDING FIELDS OF TABLE it1_col_row
  WHERE ekorg = p_ekorg
  AND fabkl = p_fabkl.

*Looping Internal table to display data
 LOOP AT it1_col_row INTO wa1_col_row.
   WRITE: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1.
 ENDLOOP.
 WRITE: /.

*Looping internal table to change rows into columns
 LOOP AT it1_col_row INTO wa1_col_row.
   CLEAR wa2_col_row.
   ASSIGN COMPONENT sy-tabix OF STRUCTURE wa2_col_row TO <fs1>.
   cline = sy-tabix.
   DO.
     ASSIGN COMPONENT sy-index OF STRUCTURE wa1_col_row TO <fs2>.
     IF sy-subrc NE 0.
       EXIT.
     ENDIF.
     IF cline = 1.
       <fs1> = <fs2>.
       APPEND wa2_col_row TO it2_col_row.
     ELSE.
       READ TABLE it2_col_row INTO wa2_col_row INDEX sy-index.
       <fs1> = <fs2>.
       MODIFY it2_col_row FROM wa2_col_row INDEX sy-index.
     ENDIF.
   ENDDO.
 ENDLOOP.

*Looping internal table to display
 LOOP AT it2_col_row INTO wa2_col_row.
   WRITE: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1.
 ENDLOOP.

 ENDFORM.

      

+2


source







All Articles