Application Development 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: 

Internal table with variable no of columns

Former Member
0 Kudos

Hi All,

I have to create an internal table with some fixed columns and rest of the table should be dynamic. The total no of columns depends on a field of some other table. hence, the number of columns of the table are unknown. How to create such a table.

Thanks,

Neha

11 REPLIES 11

Former Member
0 Kudos

Hi Neha,

You have to use field symbols to create the columns dynamically.

Regards,

jaya ram

0 Kudos

Hello Jaya ,

Can you please explain this in brief or can give one example,i am also facing same problem.

Thanks

Shri

Former Member
0 Kudos

Check this code:



FIELD-SYMBOLS : <fs_it>    TYPE table,

DATA: it_fs_it        TYPE REF TO data.
          it_tab_desc_aux TYPE lvc_t_fcat,

*fulfill the fieldcatalog with the colums you want

 CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_tab_desc_aux
    IMPORTING
      ep_table = it_fs_it.

ASSIGN it_fs_it->* TO <fs_it>.

Regards

0 Kudos

Thanks for the Replies,

Actually in this scenario, there is a field in some othwer table which gives some numeric value and acc to that the columns in the second table should be created. for eg the value in the first table is 92 , so there should be 92 columns in the second table. so how to do that.

Regards,

Neha

0 Kudos

Execute this program .. we will get a fair idea about how the dyanmic column are populated in the table. Based on that u can write ur sceond table with 92 columns

&----


*& Report ZTEST009

*&

&----


*&

*&

&----


REPORT ztest009 NO STANDARD PAGE HEADING LINE-SIZE 60 LINE-COUNT 2(1).

TYPE-POOLS : slis.

TYPES : BEGIN OF internal,

matnr(18),

werks(4),

qtyn(20),

desc(20) TYPE c,

qty TYPE i,

END OF internal.

DATA : it TYPE TABLE OF internal,

wa TYPE internal.

DATA : fieldcat TYPE lvc_t_fcat,

lcat TYPE lvc_s_fcat,

final_cat TYPE slis_t_fieldcat_alv,

fcat TYPE slis_fieldcat_alv,

top TYPE slis_t_listheader,

events TYPE slis_t_event,

layout TYPE slis_layout_alv.

DATA : newfield TYPE REF TO data,

newdata TYPE REF TO data.

FIELD-SYMBOLS : <fs1>,

<dynamic_value>,

<dynamic_cat> TYPE STANDARD TABLE.

START-OF-SELECTION.

PERFORM popudate.

PERFORM buildcat.

PERFORM loadata.

PERFORM events USING events.

PERFORM header USING top.

PERFORM layout.

END-OF-SELECTION.

PERFORM display.

&----


*& Form popudate

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM popudate .

DEFINE popu.

wa-matnr = &1.

wa-werks = &2.

wa-qtyn = &3.

wa-desc = &4.

wa-qty = &5.

append wa to it.

clear wa.

END-OF-DEFINITION.

popu 'material1' 'pla1' 'QTY1' 'quantity1' 100.

popu 'material1' 'pla1' 'QTY2' 'quantity2' 200.

popu 'material1' 'pla1' 'QTY3' 'quantity3' 300.

popu 'material2' 'pla2' 'QTY1' 'quantity1' 400.

popu 'material2' 'pla2' 'QTY2' 'quantity2' 500.

popu 'material2' 'pla2' 'QTY3' 'quantity3' 600.

popu 'material3' 'pla3' 'QTY1' 'quantity1' 700.

popu 'material3' 'pla3' 'QTY2' 'quantity2' 400.

SORT it BY matnr.

ENDFORM. " popudate

&----


*& Form buildcat

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM buildcat .

lcat-fieldname = 'MATNR'.

lcat-datatype = 'CHAR'.

lcat-seltext = 'Material'.

lcat-intlen = 18.

APPEND lcat TO fieldcat.

CLEAR lcat.

lcat-fieldname = 'WERKS'.

lcat-datatype = 'CHAR'.

lcat-seltext = 'Plant'.

lcat-intlen = 4.

APPEND lcat TO fieldcat.

CLEAR lcat.

LOOP AT it INTO wa.

READ TABLE fieldcat INTO lcat WITH KEY fieldname = wa-qtyn.

IF sy-subrc <> 0.

lcat-fieldname = wa-qtyn.

lcat-datatype = 'CHAR'.

lcat-seltext = wa-desc.

lcat-intlen = 10.

APPEND lcat TO fieldcat.

CLEAR lcat.

ENDIF.

ENDLOOP.

CLEAR lcat.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

  • i_style_table =

it_fieldcatalog = fieldcat

  • i_length_in_byte =

IMPORTING

ep_table = newfield

  • e_style_fname =

  • EXCEPTIONS

  • generate_subpool_dir_full = 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.

ASSIGN newfield->* TO <dynamic_cat>.

CREATE DATA newdata LIKE LINE OF <dynamic_cat>.

ASSIGN newdata->* TO <dynamic_value>.

ENDFORM. " buildcat

&----


*& Form loadata

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM loadata .

DATA flag TYPE i.

LOOP AT it INTO wa.

flag = 0.

ASSIGN COMPONENT 'MATNR' OF STRUCTURE <dynamic_value> TO <fs1>.

<fs1> = wa-matnr.

ASSIGN COMPONENT 'WERKS' OF STRUCTURE <dynamic_value> TO <fs1>.

<fs1> = wa-werks.

CALL FUNCTION 'AIPC_CONVERT_TO_UPPERCASE'

EXPORTING

i_input = wa-qtyn

i_langu = sy-langu

IMPORTING

e_output = wa-qtyn.

ASSIGN COMPONENT wa-qtyn OF STRUCTURE <dynamic_value> TO <fs1>.

<fs1> = wa-qty.

AT END OF matnr.

APPEND <dynamic_value> TO <dynamic_cat>.

CLEAR : <dynamic_value>.

ENDAT.

CLEAR : wa.

ENDLOOP.

ENDFORM. " loadata

*&----


**& Form display

*&----


    • text

*----


    • --> p1 text

    • <-- p2 text

*----


FORM display .

LOOP AT fieldcat INTO lcat.

fcat-fieldname = lcat-fieldname.

fcat-outputlen = lcat-intlen.

fcat-seltext_l = lcat-seltext.

APPEND fcat TO final_cat.

ENDLOOP.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-repid

    • I_CALLBACK_PF_STATUS_SET = ' '

    • I_CALLBACK_USER_COMMAND = ' '

is_layout = layout

it_fieldcat = final_cat

    • IT_SORT =

    • I_SAVE = ' '

    • IS_VARIANT =

it_events = events

TABLES

t_outtab = <dynamic_cat>

.

ENDFORM. " display

Regards,

Aswin.

0 Kudos

In that case you have to add 92 lines to the fieldcatalog table, with the metatada of your columns. On the line of the comment *fullfill fieldcatalog.

Example lets suppose we want (no_columns) of type carrid.



DATA: no_columns type i.
no_columns = 92.

DO no_columns TIMES.

it_tab_desc_aux-fieldname = 'cARRID'.
it_tab_desc_aux-ref_field = 'cARRID'.
it_tab_desc_aux-ref_field = 'SFLIGHT'.
APPEND it_tab_desc_aux.

ENDDO.

Regards.

Former Member
0 Kudos

Hi,

use FM lvc_table_create, pass the class name and call it after like below..

cl_alv_table_create-->create_dynamic_table.

Hope it helps!!

Regards,

Pavan

Former Member
0 Kudos

Hi..

Try Below code.

DATA: IT_LVC_T_FCAT TYPE LVC_T_FCAT,

IS_LVC_T_FCAT LIKE LINE OF IT_LVC_T_FCAT.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,

<DYN_WA>,

<DYN_FIELD>.

  • Create dynamic internal table and assign to FS

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_LVC_T_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>.

Salil ...

Edited by: salil chavan on Apr 2, 2009 6:42 AM

Former Member
0 Kudos

Thanks,

the problem is solved now

amosweiser
Member
0 Kudos

Try this code:

DATA: dy_table TYPE REF TO data,

dy_line TYPE REF TO data.

FIELD-SYMBOLS: <dyn_table> TYPE ANY TABLE,

<dyn_wa>.

CREATE DATA dy_table TYPE STANDARD TABLE OF (fill Table name here).

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

Former Member
0 Kudos

HI,

assume that you want to have a report that user able to select table name (for example mara ,...) and then system displays data in ALV to user.

write bellow codes:

====================================

data: QUERY_TABLE LIKE  DD02L-TABNAME .

PARAMETERS :
p_tab LIKE QUERY_TABLE .


DATA: BEGIN OF WORK, BUFFER(30000), END OF WORK,

                    DREF TYPE REF TO DATA.

 
FIELD-SYMBOLS: <WA> TYPE ANY, <COMP> TYPE ANY.

FIELD-SYMBOLS: <ITAB> TYPE STANDARD TABLE.

START-OF-SELECTION .

QUERY_TABLE = p_tab . "table name

ASSIGN WORK TO <WA> CASTING TYPE (QUERY_TABLE).

CREATE DATA  DREF   TYPE STANDARD TABLE OF (QUERY_TABLE).

ASSIGN   DREF->* TO <ITAB>.

SELECT   * FROM (QUERY_TABLE) UP TO i_rows rows INTO TABLE <ITAB> .

regards ,

REZA ROSTAMI / SAPHIRAN(www.saphiran.com) ABAPER