Skip to Content

How to convert rows to lines in internal table?

Hi Folks,

i have an internal table with some rows and would like to convert that table into another internal table with columns.

Let me give you an example:

it1:

a

b

c

d

it1 should be converted into table it2 and should look like this:

it2:

a b c d

The problem is that the entries in table it1 are not fixed, so that means that the number of lines is different depending on the query...

Has anyone an idea?

Thanks in advance,

Ralf

Edited by: Ralf Vath on Oct 17, 2008 11:01 AM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • Best Answer
    Oct 17, 2008 at 09:18 AM

    hi i have an example in the alv ....that the rows are transported into columns...

    REPORT Z_TRANSPOSEALV .* Type pools declaration for ALV

    TYPE-POOLS: slis.*Declarations for ALV, dynamic table and col no for transpose

    DATA: l_col TYPE sy-tabix,

    l_structure TYPE REF TO data,

    l_dyntable TYPE REF TO data,

    wa_lvc_cat TYPE lvc_s_fcat,

    lt_lvc_cat TYPE lvc_t_fcat,

    lt_fieldcatalogue TYPE slis_t_fieldcat_alv,

    wa_fieldcat TYPE slis_fieldcat_alv,

    lt_fieldcat TYPE slis_t_fieldcat_alv,

    lt_layout TYPE slis_layout_alv.*Field symbols declarations

    FIELD-SYMBOLS :

    <header> TYPE ANY,

    <dynheader> TYPE ANY,

    <dyndata> TYPE ANY,

    <ls_table> TYPE ANY,

    <dynamictable> TYPE STANDARD TABLE,

    <it_table> TYPE STANDARD TABLE.*Input the name of the table

    PARAMETERS p_table TYPE dd02l-tabname OBLIGATORY.*Initialization event

    INITIALIZATION.*Start of selection event

    START-OF-SELECTION.* Create internal table of dynamic type

    CREATE DATA l_dyntable TYPE STANDARD TABLE OF (p_table)

    WITH NON-UNIQUE DEFAULT KEY.

    ASSIGN l_dyntable->* TO <it_table>.*select statement to select data from the table as input into

    *our dynamic internal table.

    *Here i have restricted only till 5 rows.

    *You can set a variable and give no of rows to be fetched

    *The variable can be set in your select statement SELECT * INTO CORRESPONDING FIELDS OF TABLE <it_table>

    FROM (p_table) up to 5 rows.*Fieldcatalogue definitions

    wa_lvc_cat-fieldname = 'COLUMNTEXT'.

    wa_lvc_cat-ref_table = 'LVC_S_DETA'.

    APPEND wa_lvc_cat TO lt_lvc_cat. wa_fieldcat-fieldname = 'COLUMNTEXT'.

    wa_fieldcat-ref_tabname = 'LVC_S_DETA'.

    wa_fieldcat-key = 'X'..

    APPEND wa_fieldcat TO lt_fieldcat. DESCRIBE TABLE <it_table>. DO sy-tfill TIMES.

    • For each line, a column 'VALUEx' is created in the fieldcatalog

    • Build Fieldcatalog

    WRITE sy-index TO wa_lvc_cat-fieldname LEFT-JUSTIFIED.

    CONCATENATE 'VALUE' wa_lvc_cat-fieldname

    INTO wa_lvc_cat-fieldname.

    wa_lvc_cat-ref_field = 'VALUE'.

    wa_lvc_cat-ref_table = 'LVC_S_DETA'.

    APPEND wa_lvc_cat TO lt_lvc_cat.

    • Build Fieldcatalog

    CLEAR wa_fieldcat.

    wa_fieldcat-fieldname = wa_lvc_cat-fieldname.

    wa_fieldcat-ref_fieldname = 'VALUE'.

    wa_fieldcat-ref_tabname = 'LVC_S_DETA'.

    APPEND wa_fieldcat TO lt_fieldcat.

    ENDDO.* Create dynamic internal table

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = lt_lvc_cat

    IMPORTING

    ep_table = l_dyntable. ASSIGN l_dyntable->* TO <dynamictable>.* Create structure as structure of the internal table

    CREATE DATA l_structure LIKE LINE OF <dynamictable>.

    ASSIGN l_structure->* TO <header>.* Create structure = structure of the internal table

    CREATE DATA l_structure LIKE LINE OF <it_table>.

    ASSIGN l_structure->* TO <ls_table>.* Create field catalog from our table structure

    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

    EXPORTING

    i_structure_name = p_table

    CHANGING

    ct_fieldcat = lt_fieldcatalogue

    EXCEPTIONS

    inconsistent_interface = 1

    program_error = 2

    OTHERS = 3.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ENDIF. DESCRIBE TABLE lt_fieldcatalogue.* Fill the internal to display <dynamictable>

    DO sy-tfill TIMES.

    IF sy-index = 1.

    READ TABLE lt_fieldcatalogue INTO wa_fieldcat INDEX 1.

    ENDIF.

    • For each field of it_table

    ASSIGN COMPONENT 1 OF STRUCTURE <header> TO <dynheader>.

    IF sy-subrc NE 0. EXIT .ENDIF.

    READ TABLE lt_fieldcatalogue INTO wa_fieldcat INDEX sy-index.

    • Fill 1st column

    <dynheader> = wa_fieldcat-seltext_m.

    IF <dynheader> IS INITIAL.

    <dynheader> = wa_fieldcat-fieldname.

    ENDIF.*Filling the other columns

    LOOP AT <it_table> INTO <ls_table>.

    l_col = sy-tabix + 1.

    ASSIGN COMPONENT sy-index OF STRUCTURE <ls_table> TO <dyndata>.

    IF sy-subrc NE 0. EXIT .ENDIF.

    ASSIGN COMPONENT l_col OF STRUCTURE <header> TO

    <dynheader>.

    IF sy-subrc NE 0. EXIT .ENDIF.

    WRITE <dyndata> TO <dynheader> LEFT-JUSTIFIED.

    ENDLOOP.

    APPEND <header> TO <dynamictable>.

    ENDDO.*Layout for ALV output

    lt_layout-zebra = 'X'.

    lt_layout-no_colhead = 'X'..

    lt_layout-colwidth_optimize ='X'.

    lt_layout-window_titlebar = 'ALV GRID TRANSPOSED'.*ALV Grid output for display

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    is_layout = lt_layout

    it_fieldcat = lt_fieldcat

    TABLES

    t_outtab = <dynamictable>.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 17, 2008 at 09:10 AM

    HI

    Why dont you try with the dynamic internal table . This concept will solve your problem

    Thanks,

    Chidanand

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 17, 2008 at 09:10 AM

    using Field symbols and Dynamic internal table you can do it. how many rows it1 will have ..? is it fixed or dynamic. based on it you can build a dynamic table and assign them to a internal table single row.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 09:14 AM

    Hi,

    Take a look at the program BCALV_TABLE_CREATE.

    You can loop at your itab1 and create the fieldcatalog and then create a dynamic table itab2 and then again loop at the itab1 and assign each record of itab1 to the column of itab2.

    regards,

    Advait.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 09:14 AM

    Hi, You can use field symbols to do that.

    Use the below code:

     
    Data: begin of itab occurs 0,
          row ,
          end   of itab,
    
          begin of otab occurs 0,
          fld1,
          fld2,
          fld3,
          fldn,
          end   of otab.
    
    field-symbols: <fs> type any.
    
    * Append rows
    itab-row = 'A'.
    append itab.
    
    itab-row = 'B'.
    append itab.
    
    itab-row = 'C'.
    append itab.
    
    itab-row = 'D'.
    append itab.
    
    
    loop at itab.
    
    * Assigning row to column
    assign component sy-tabix of structure otab to <fs>.
    <fs> = itab-row.
    
    endloop.
    
    *Append Table 2
    append otab.
    
    write otab.
    

    Regards,

    Jey

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 09:23 AM

    Hello,

    This is little bit complex logic try to understand first then you start implement it.

    It is working fine for me so can use it no problem

    1)

    First identify the no. of rows in the internal table.

    LOOP AT y_i_out INTO y_wa_out

    WHERE instance = y_wa_final_output-cuobj.

    ADD 1 TO y_v_cntr.

    ENDLOOP. "LOOP AT y_i_out INTO y_wa_out .

    2)Create the Field catalog.

    DO y_v_cntr TIMES.

    y_v_charno = y_v_charno + 1.

    y_v_charno = y_v_charno + 1.

    y_wa_fcat-ref_field = 'ATNAM'.

    y_wa_fcat-ref_table = 'CABN'.

    CONCATENATE 'ATNAM' '-'

    y_v_charno

    INTO y_wa_fcat-fieldname.

    CONCATENATE text-037

    y_v_charno

    INTO y_wa_fcat-scrtext_m .

    APPEND y_wa_fcat TO y_i_fcat.

    CLEAR y_wa_fcat.

    ENDDO. "DO y_lv_cntr_max TIMES.

    3)Create the Dynamic internal table.

    FIELD-SYMBOLS : <y_fld> TYPE ANY.

    DATA: y_lv_comp TYPE string.

    • create dynamic internal table and assign to fs

    • CREATE OBJECT y_obj.

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = y_i_fcat

    IMPORTING

    ep_table = dy_table.

    ASSIGN dy_table->* TO <dyn_table>.

    • create dynamic work area and assign to fs

    CREATE DATA dy_line LIKE LINE OF <dyn_table>.

    ASSIGN dy_line->* TO <dyn_wa>.

    4) move the output values to

    LOOP AT y_i_out INTO y_wa_out

    CONCATENATE 'ATNAM' '-'

    y_v_charno

    INTO y_lv_comp.

    ASSIGN COMPONENT y_lv_comp OF STRUCTURE <dyn_wa> TO <y_fld> .

    <y_fld> = y_wa_out-atnam.

    ENDLOOP. " LOOP AT y_i_out INTO y_wa_out

    APPEND <dyn_wa> TO <dyn_table>.

    FREE: <dyn_wa>, <y_fld> .

    CLEAR y_v_charno.

    5)Creat one more Field catalog.

    LOOP AT y_i_fcat INTO y_wa_fcat.

    MOVE: y_wa_fcat-fieldname TO y_wa_fieldcat-fieldname,

    y_wa_fcat-tabname TO y_wa_fieldcat-tabname,

    y_wa_fcat-col_pos TO y_wa_fieldcat-col_pos,

    y_wa_fcat-scrtext_m TO y_wa_fieldcat-seltext_m,

    y_wa_fcat-datatype TO y_wa_fieldcat-datatype,

    y_wa_fcat-intlen TO y_wa_fieldcat-intlen,

    y_wa_fcat-decimals_o TO y_wa_fieldcat-decimals_out.

    APPEND y_wa_fieldcat TO y_i_fieldcat.

    6) Pass the values to ALV Grid.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    i_callback_program = sy-repid

    is_layout = y_wa_layo

    it_fieldcat = y_i_fieldcat

    it_events = y_i_alv_evnt_tb_cmpl

    TABLES

    t_outtab = <dyn_table>

    EXCEPTIONS

    program_error = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ENDIF.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 09:31 AM

    Hello,

    You can do this using two classes:

    CL_ABAP_STRUCTDESCR

    CL_ALV_TABLE_CREATE.

    The first class is used to get the fields of the internal table using and the second method is used to create internal table using those fields dynamically.

    Edited by: Rakesh More on Oct 17, 2008 11:31 AM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 20, 2008 at 04:38 PM

    Hello Ralf,

    I assume the solution from Jey should be sufficient.

    But if you like to create a table with columns which are rows in another table, please try out this piece of coding. I used the data type 'String', but you could adjust this to your needs.

    DATA: struct_ref TYPE REF TO cl_abap_structdescr.
    DATA: table_ref TYPE REF TO cl_abap_tabledescr.
    DATA: t_comp TYPE abap_component_tab.
    DATA: wa_comp type line of abap_component_tab.
    DATA: dref_line TYPE REF TO data.
    DATA: dref_tab TYPE REF TO data.
    DATA: wa TYPE string.
    DATA: itab TYPE TABLE OF string.
    FIELD-SYMBOLS: <trans_itab> TYPE STANDARD TABLE.
    FIELD-SYMBOLS: <trans_wa> TYPE ANY.
    
    START-OF-SELECTION.
    * fill itab
    wa = 'A'.
    append wa to itab.
    wa = 'B'.
    append wa to itab.
    wa = 'C'.
    append wa to itab.
    wa = 'D'.
    append wa to itab.
    wa = 'E'.
    append wa to itab.
    
    * Build target table structure
    loop at itab into wa.
      wa_comp-name = wa.
      wa_comp-type = cl_abap_elemdescr=>get_string( ).
      append wa_comp to t_comp.
    endloop.
    
    struct_ref = cl_abap_structdescr=>create( t_comp ).
    table_ref = cl_abap_tabledescr=>create( struct_ref ).
    
    * Create own data line object
      CREATE DATA dref_line TYPE HANDLE struct_ref.
      ASSIGN dref_line->* TO <trans_wa>.
    * Create own data table object
      CREATE DATA dref_tab TYPE HANDLE table_ref.
      ASSIGN dref_tab->* TO <trans_itab>.
    

    But the content of the target table <trans_tab> is empty. The workarea is accessible with <trans_wa>.

    Kind regards

    Riccardo

    Add comment
    10|10000 characters needed characters exceeded