如何将内部表行转换为列?

时间:2014-11-27 05:50:53

标签: sap abap

我想将我的内部表行转换为列,我想修复第一列,我试图使用以下代码,但我没有得到预期的结果....它没有转换所有的行成列

*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.

1 个答案:

答案 0 :(得分:2)

请注意,ty_t001w的字段类型具有不同的长度:

  • ekorg TYPE t001w-ekorgCHAR 4
  • werks TYPE t001w-werks也有CHAR 4,但
  • name1 TYPE t001w-name1CHAR 30

您对源表(ty_t001w)以及目标表(it1_col_row)使用相同类型it2_col_row。因此,当您将源行表映射到目标列表时,30个字符的字段name1将映射到4个字符的字段ekorg 。当我在我的系统中执行你的程序时,我有以下输出(取决于我的数据库表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

乍一看,这看起来像&#34;它没有将所有行转换为列#34;。但是在调试器中我注意到了#34; Werk 0001&#34;实际上是一个值,而不是两个!但是,该值仅被截断为&#34; Werk&#34;因为它从30个字符的字段映射到4个字符的字段。这发生在第1列(&#34; Werk 0002&#34;)和2(&#34; Werk 0003&#34;)的最低值。第3列的底部值(&#34; Werk RAD1&#34;)已正确映射,因为它在此处从30个字符字段映射到30个字符字段。

要解决此问题,我已为目标表TYPES创建了额外的ty_t001w_col定义it2_col_row。在此TYPE中,所有字段的最大长度均为30个字符,确保不会发生截断(请参阅下面的abap代码)。它生成以下输出:

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

更正的报告:

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.