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

Converting Internal table columns to rows

Former Member
0 Likes
8,198

Hi,

i have an dynamic internal table(no.of records are changing each time...suppose for one condition it will have 9 records and in other condition it is having 4 records etc. this internal table is keep on changing)

Eg, internal table records appears as:

AB

ACW

AD

AE

A

AG

AHO

now i have to change this internal table to

AB ACW AD AE A AG AHO

here in the above case i have 7 columns and 1 row in my internal table. now i have to change this internal table to 1 column and 7 rows.(concatenating and appending to String may be helpful but it comes in a single line but i need to convert them as no.of columns = no.of rows).

my internal table structure is having only one field.

data: begin of type_table,

col(4) type c,

end of type_table.

iam handling this in smartforms so field catalog is not helpful to me.

<REMOVED BY MODERATOR>

Thanks,

Sree

Edited by: Alvaro Tejada Galindo on Jul 9, 2008 11:08 AM

5 REPLIES 5
Read only

Former Member
0 Likes
3,678

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.

Read only

Former Member
0 Likes
3,678

If you want to use field symbols then this is the one....

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.

Read only

Former Member
0 Likes
3,678

Hi,

here we dont know how many fields that are coming to first internal table so declaring second internal table with fixed fields is not possible in this case.

is there any way to design an internal table based on the no.of records getting from the first internal table.

Thanks,

Sree.

Read only

0 Likes
3,678

Please check this code...

FIELD-SYMBOLS: <dyntable> TYPE table.

FIELD-SYMBOLS: <dynline> TYPE ANY.

FIELD-SYMBOLS: <fs_1>, <fs>.

FIELD-SYMBOLS:

<dyn_wa>,

<dyn_field>.

DATA: lt TYPE lvc_t_fcat. "table of field cat structure

DATA: ls TYPE lvc_s_fcat. "Field cat structure

DATA: new_table TYPE REF TO data,

new_line TYPE REF TO data.

TYPES: BEGIN OF periodtyp,

period(10) TYPE c,

total TYPE netwr,

END OF periodtyp.

TYPES: BEGIN OF itab_typ,

period01 TYPE netwr,

period02 TYPE netwr,

period03 TYPE netwr,

period04 TYPE netwr,

period05 TYPE netwr,

period06 TYPE netwr,

period07 TYPE netwr,

period08 TYPE netwr,

period09 TYPE netwr,

period10 TYPE netwr,

period11 TYPE netwr,

period12 TYPE netwr,

END OF itab_typ.

DATA:

wa_period TYPE periodtyp,

wa1 TYPE itab_typ,

sum TYPE itab_typ,

w_fieldname(20) TYPE c,

c_tabix(2) TYPE n,

w_count TYPE i,

count(2) TYPE n.

DATA: it_period TYPE TABLE OF periodtyp,

itab1 TYPE TABLE OF itab_typ.

w_count = 0.

PERFORM load_itab1.

SKIP.

ULINE.

SKIP.

PERFORM create_dyntab.

PERFORM populating_dyntable.

PERFORM display_dyntab_data.

&----


*& Form CREATE_DYNTAB

&----


text

-


FORM create_dyntab.

Build field catalog of dynamic structure.

LOOP AT it_period INTO wa_period.

IF wa_period-total > 0.

c_tabix = sy-tabix.

CONCATENATE 'PERIOD' c_tabix INTO w_fieldname.

ls-fieldname = w_fieldname.

ls-datatype = 'CURR'.

ls-tabname = 'NEWTAB'.

ls-intlen = 10.

WRITE: / 'Field:', w_fieldname, 30 wa_period-total.

APPEND ls TO lt.

ENDIF.

ENDLOOP.

Create dynamic internal table with dynamic structure

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = new_table.

*-Assign dynamic table to field symbol.

ASSIGN new_table->* TO <fs_1>.

ASSIGN <fs_1> TO <dyntable>.

Create a new Line with the same structure of the table.

CREATE DATA new_line LIKE LINE OF <dyntable>.

ASSIGN new_line->* TO <dynline>.

ENDFORM. "CREATE_DYNTAB

&----


*& Form load_itab1

&----


text

-


FORM load_itab1.

DO 5 TIMES.

CLEAR wa1.

MOVE: 100 TO wa1-period01,

15 TO wa1-period02,

20 TO wa1-period03,

30 TO wa1-period05,

7 TO wa1-period07.

IF sy-index = 3.

wa1-period02 = 0.

wa1-period07 = 0.

ENDIF.

APPEND wa1 TO itab1.

ENDDO.

LOOP AT itab1 INTO wa1.

ADD:

wa1-period01 TO sum-period01,

wa1-period02 TO sum-period02,

wa1-period03 TO sum-period03,

wa1-period04 TO sum-period04,

wa1-period05 TO sum-period05,

wa1-period06 TO sum-period06,

wa1-period07 TO sum-period07,

wa1-period08 TO sum-period08,

wa1-period09 TO sum-period09,

wa1-period10 TO sum-period10,

wa1-period11 TO sum-period11,

wa1-period12 TO sum-period12.

ENDLOOP.

CLEAR wa_period.

MOVE: sum-period01 TO wa_period-total,

'PERIOD01' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period02 TO wa_period-total,

'PERIOD02' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period03 TO wa_period-total,

'PERIOD03' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period04 TO wa_period-total,

'PERIOD04' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period05 TO wa_period-total,

'PERIOD05' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period06 TO wa_period-total,

'PERIOD06' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period07 TO wa_period-total,

'PERIOD07' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period08 TO wa_period-total,

'PERIOD08' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period09 TO wa_period-total,

'PERIOD09' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period10 TO wa_period-total,

'PERIOD10' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period11 TO wa_period-total,

'PERIOD11' TO wa_period-period.

APPEND wa_period TO it_period.

MOVE: sum-period12 TO wa_period-total,

'PERIOD12' TO wa_period-period.

APPEND wa_period TO it_period.

WRITE: / 'Input table itab1',

/4 'PREIOD01', 15 'PREIOD02', 26 'PREIOD03', 37 'PREIOD04',

48 'PREIOD05', 59 'PREIOD06', 70 'PREIOD07', 81 'PREIOD08',

92 'PREIOD09', 103 'PREIOD10', 114 'PREIOD11', 125 'PREIOD12'.

LOOP AT itab1 INTO wa1.

WRITE: / ' ',

(10) wa1-period01, (10) wa1-period02 , (10) wa1-period03,

(10) wa1-period04 , (10) wa1-period05, (10) wa1-period06,

(10) wa1-period07, (10) wa1-period08, (10) wa1-period09,

(10) wa1-period10, (10) wa1-period11, (10) wa1-period12.

ENDLOOP.

ULINE.

WRITE: / ' ',

(10) sum-period01, (10) sum-period02, (10) sum-period03,

(10) sum-period04, (10) sum-period05, (10) sum-period06,

(10) sum-period07, (10) sum-period08, (10) sum-period09,

(10) sum-period10, (10) sum-period11, (10) sum-period12.

ULINE.

WRITE: / 'Table it_period for 12 periods'. SKIP.

LOOP AT it_period INTO wa_period.

WRITE: / wa_period-period, wa_period-total.

ENDLOOP.

ENDFORM. "load_itab1

&----


*& Form populating_dyntable

&----


text

-


FORM populating_dyntable.

DATA: fieldname(20) TYPE c.

DATA: fieldvalue(10) TYPE c,

fval(10) TYPE c.

DATA: index(2) TYPE c.

FIELD-SYMBOLS: <fs_con>.

DATA: w_con(50).

  • Populate data to dynamic table from itab1.

SORT it_period BY period.

WRITE: / 'Dynamic table data:'. SKIP.

LOOP AT itab1 INTO wa1.

LOOP AT it_period INTO wa_period.

IF wa_period-total > 0.

c_tabix = sy-tabix.

CONCATENATE 'PERIOD' c_tabix INTO w_fieldname.

CONDENSE w_fieldname NO-GAPS.

ASSIGN COMPONENT w_fieldname OF STRUCTURE <dynline> TO <fs>.

ASSIGN COMPONENT w_fieldname OF STRUCTURE wa1 TO <fs_1>.

<fs> = <fs_1>.

ENDIF.

ENDLOOP.

INSERT <dynline> INTO TABLE <dyntable>.

ENDLOOP.

ENDFORM. "populating_dyntable

&----


*& Form display_dyntab

&----


text

-


FORM display_dyntab_data.

Display dynamic table

DATA: l_components TYPE i,

comp_idx TYPE i,

t TYPE c.

TYPE-POOLS: sydes.

DATA td TYPE sydes_desc.

FIELD-SYMBOLS: <fs1> TYPE sydes_desc.

LOOP AT <dyntable> INTO <dynline>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <dynline> TO <dyn_field>.

IF sy-subrc NE 0.

EXIT.

ELSE.

IF sy-index = 1.

WRITE: / <dyn_field>.

ELSE.

WRITE: <dyn_field>.

ENDIF.

ENDIF.

ENDDO.

ENDLOOP.

ENDFORM. "display_dyntab_data

Read only

Hamad
Explorer
0 Likes
2,642

Solution!

 

DATA:
  cs_zsnps4va_t_dif TYPE glt0_bck,
  ls_flexa          TYPE v_faglflexa_ori,
  lt_flexa          TYPE TABLE OF v_faglflexa_ori.

cs_zsnps4va_t_dif-bukrs = 1710.
cs_zsnps4va_t_dif-tsl01 = -100.
cs_zsnps4va_t_dif-tsl02 = 200.
cs_zsnps4va_t_dif-tsl03 = 300.
cs_zsnps4va_t_dif-tsl04 = 400.
cs_zsnps4va_t_dif-hsl01 = -500.
cs_zsnps4va_t_dif-hsl02 = 600.
cs_zsnps4va_t_dif-hsl03 = 000.
cs_zsnps4va_t_dif-hsl04 = 800.
cs_zsnps4va_t_dif-ksl01 = -900.
cs_zsnps4va_t_dif-ksl02 = 1000.
cs_zsnps4va_t_dif-ksl03 = 1100.
cs_zsnps4va_t_dif-ksl04 = -1200.

DATA: lv_belnr TYPE belnr_d VALUE 'B000000000'.
SELECT *
  INTO TABLE (lt_tka02)
  FROM tka02
  WHERE bukrs = _zsnps4va_t_dif-bukrs.

SELECT *
  INTO TABLE (lt_finsc_ld_cmp)
  FROM finsc_ld_cmp
  WHERE bukrs = _zsnps4va_t_dif-bukrs
  AND   rldnr NE '0L'.

DO 12 TIMES.

  IF sy-index = 1 OR sy-index = 5 OR sy-index = 9.
    DATA(lv_index) = '01'.
    IF sy-index = 1.
      DATA(col) = 'TSL'.
    ELSEIF sy-index = 5.
      col = 'HSL'.
    ELSEIF sy-index = 9.
      lv_index = '01'.
      col = 'KSL'.
    ENDIF.
  ENDIF.

  DATA(lv_field_s)  = |cs_zsnps4va_t_dif-{ col }{ lv_index }|.
  DATA(lv_field_t)  = |ls_flexa-{ col }|.

  CONDENSE: lv_field_s NO-GAPS,
            lv_field_t NO-GAPS.

  FIELD-SYMBOLS <fs_amt_s>.
  FIELD-SYMBOLS <fs_amt_t>.
  ASSIGN (lv_field_s) TO <fs_amt_s>.
  ASSIGN (lv_field_t) TO <fs_amt_t>.

  " Check if the assignment was successful
  IF sy-subrc = 0 AND <fs_amt_s> IS NOT INITIAL.

    lv_belnr =  lv_belnr+1(9) + 1.
    UNPACK lv_belnr TO lv_belnr.
    lv_belnr = |B{ lv_belnr+1(9) }|.

*Add Values to Flexa
    ls_flexa-rbukrs = cs_zsnps4va_t_dif-bukrs.
    ls_flexa-kokrs = VALUE #(
                              lt_tka02[
                                        bukrs = cs_zsnps4va_t_dif-bukrs
                                      ]-kokrs OPTIONAL ).
    "Get the value dynamically
    <fs_amt_t> = <fs_amt_s>.
    ls_flexa-poper = lv_index.
    IF <fs_amt_t> > 0.
      ls_flexa-drcrk = 'S'.
      ls_flexa-bschl = '40'.
    ELSE.
      ls_flexa-drcrk = 'H'.
      ls_flexa-bschl = '50'.
    ENDIF.

    ls_flexa-docnr  = lv_belnr.
    ls_flexa-belnr  = lv_belnr.
    ls_flexa-docln  = '000001'.
    ls_flexa-buzei  = '001'.
    ls_flexa-rclnt = sy-mandt.
    ls_flexa-ryear = cs_zsnps4va_t_dif-ryear.
    ls_flexa-rbukrs = cs_zsnps4va_t_dif-bukrs.
    ls_flexa-racct = cs_zsnps4va_t_dif-racct.
    ls_flexa-rwcur = cs_zsnps4va_t_dif-rtcur.
    ls_flexa-gjahr = cs_zsnps4va_t_dif-ryear.
    ls_flexa-budat = sy-datum.
    ls_flexa-prctr = 'MIG1'.
    ls_flexa-rldnr = '0L'.
    ls_flexa-rtcur = cs_zsnps4va_t_dif-rtcur.
    ls_flexa-usnam = 'SAP_RFC'.
    GET TIME STAMP FIELD ls_flexa-timestamp.

    APPEND ls_flexa TO lt_flexa.

    DATA(lv_count) = lines( lt_finsc_ld_cmp ).
    DO lv_count TIMES.
      ls_flexa-rldnr = VALUE #(
                                lt_finsc_ld_cmp[
                                                sy-index
                                                ]-rldnr OPTIONAL ).
      APPEND ls_flexa TO lt_flexa.
    ENDDO.

  ENDIF.
  CLEAR: lv_field_s, lv_field_t,<fs_amt_t>.
  ADD 1 TO lv_index.
  IF lv_index < 10.
    lv_index = |0{ lv_index }|.
  ENDIF.
ENDDO.

 

Hamad