sap - How to transpose an internal table rows into columns? -
i want transpose internal table rows column , want fix first column,i trying following code not getting expected result....it not converting rows 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 , 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 data select * t001w corresponding fields of table it1_col_row ekorg = p_ekorg , fabkl = p_fabkl. **looping internal table display data loop @ it1_col_row wa1_col_row. write: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1. endloop. write: /. **looping internal table change rows columns loop @ it1_col_row wa1_col_row. clear wa2_col_row. assign component sy-tabix of structure wa2_col_row <fs1>. cline = sy-tabix. do. assign component sy-index of structure wa1_col_row <fs2>. if sy-subrc ne 0. exit. endif. if cline = 1. <fs1> = <fs2>. append wa2_col_row it2_col_row. else. read table it2_col_row wa2_col_row index sy-index. <fs1> = <fs2>. modify it2_col_row wa2_col_row index sy-index. endif. enddo. endloop. * **looping internal table display loop @ it2_col_row wa2_col_row. write: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1. endloop.
notice field types of ty_t001w
have different length:
ekorg type t001w-ekorg
haschar 4
werks type t001w-werks
haschar 4
, butname1 type t001w-name1
haschar 30
you using same type ty_t001w
source table (it1_col_row
) target table (it2_col_row
). when mapping source rows table target columns table 30 character field name1
mapped 4 character field ekorg
. when executed program in system had following output (dependent on contents of 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 looks "it not converting rows columns". in debugger noticed "werk 0001" one value, not two! value truncated "werk" because mapped 30 character field 4 character field. happens bottom value of column 1 ("werk 0002") , 2 ("werk 0003"). bottom value of column 3 ("werk rad1") mapped correctly because here it's mapped 30 character field 30 character field.
to correct issue have created types
definition ty_t001w_col
target table it2_col_row
. in type
fields have maximum length of 30 characters ensuring no truncation may occur (see abap code below). generates 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
the 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 , 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 data select * t001w corresponding fields of table it1_col_row ekorg = p_ekorg , fabkl = p_fabkl. *looping internal table display data loop @ it1_col_row wa1_col_row. write: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1. endloop. write: /. *looping internal table change rows columns loop @ it1_col_row wa1_col_row. clear wa2_col_row. assign component sy-tabix of structure wa2_col_row <fs1>. cline = sy-tabix. do. assign component sy-index of structure wa1_col_row <fs2>. if sy-subrc ne 0. exit. endif. if cline = 1. <fs1> = <fs2>. append wa2_col_row it2_col_row. else. read table it2_col_row wa2_col_row index sy-index. <fs1> = <fs2>. modify it2_col_row wa2_col_row index sy-index. endif. enddo. endloop. *looping internal table display loop @ it2_col_row wa2_col_row. write: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1. endloop. endform.
Comments
Post a Comment