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

Dynamic internal tablw

Former Member
0 Likes
880

hi friends,do anyone of u have any matereial or doc to get familiar to create Dynamic internal table in sap,

please send me.

Regards,

Deepak Sodhi - Siemens.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
841

You can create dynamic internal tables using..

field-symbols: <table> type any.

types: fieldref type ref to data.

data: dyn_table type fieldref.

As for the actual code to generate the table:

create data dyn_table type (SAP Table).

assign dyn_table->* to table.

REPORT zselect_dynamic LINE-SIZE 132

LINE-COUNT 65(1)

NO STANDARD PAGE HEADING

MESSAGE-ID z1.

TYPES ztab LIKE dcobjdef-name .

PARAMETERS: tab_name TYPE ztab DEFAULT 'Z?????' ,

tab_nam2 TYPE ztab DEFAULT 'Z?????' ,

pclient AS CHECKBOX .

SELECTION-SCREEN SKIP .

PARAMETERS: where1(80) ,

where2(80) ,

where3(80) ,

where4(80) .

*

DATA : lcode(72),

prog_tab LIKE lcode OCCURS 0 WITH HEADER LINE .

DEFINE append_line.

append &1 to prog_tab.

END-OF-DEFINITION.

DATA: BEGIN OF nametab OCCURS 0.

INCLUDE STRUCTURE dntab.

DATA: END OF nametab.

DATA: BEGIN OF twhere OCCURS 20,

line(80),

END OF twhere.

DATA: zprogram LIKE sy-cprog,

no_line TYPE i,

zmessage(150) ,

count_commit TYPE i .

DATA: d_ref TYPE REF TO data,

d_ref2 TYPE REF TO data ,

lt_alv_cat TYPE TABLE OF lvc_s_fcat,

ls_alv_cat LIKE LINE OF lt_alv_cat.

FIELD-SYMBOLS : TYPE table,

,

,"TYPE ANY ,

,

.

**----


    • Main program.

**----


START-OF-SELECTION.

END-OF-SELECTION.

**----


    • Main program.

**----


PERFORM z_define_itab .

&----


*& Form z_define_itab

&----


FORM z_define_itab .

CHECK ( tab_name(01) = 'Z' OR tab_name(01) = 'Y' )

  • if you want treat tables with your namespace, insert here your code

AND ( tab_nam2(01) = 'Z' OR tab_nam2(01) = 'Y' ) .

REFRESH nametab.

CALL FUNCTION 'NAMETAB_GET'

EXPORTING

langu = sy-langu

tabname = tab_name

TABLES

nametab = nametab

EXCEPTIONS

no_texts_found = 1.

LOOP AT nametab .

ls_alv_cat-fieldname = nametab-fieldname .

ls_alv_cat-ref_table = tab_name.

ls_alv_cat-ref_field = nametab-fieldname .

APPEND ls_alv_cat TO lt_alv_cat.

ENDLOOP.

  • internal table build

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_alv_cat

IMPORTING ep_table = d_ref .

ASSIGN d_ref->* TO .

IF where1 IS INITIAL AND where2 IS INITIAL

AND where3 IS INITIAL AND where4 IS INITIAL .

SELECT * FROM (tab_name) INTO TABLE .

  • ORDER BY PRIMARY KEY.

ELSE .

PERFORM select_with_where .

ENDIF .

DESCRIBE TABLE LINES sy-tfill .

IF sy-tfill = 0 .

MESSAGE i000 WITH

'Data not selected, verify the tables or conditions ! ' .

STOP .

ELSE .

MESSAGE s000 WITH 'You have successfully treated yours tables.'.

ENDIF .

CREATE DATA d_ref2 TYPE (tab_nam2).

ASSIGN d_ref2->* TO .

LOOP AT ASSIGNING .

CLEAR .

LOOP AT nametab .

ASSIGN COMPONENT nametab-fieldname

OF STRUCTURE TO .

IF sy-subrc <> 0. EXIT. ENDIF.

ASSIGN COMPONENT nametab-fieldname OF STRUCTURE TO .

IF sy-subrc = 0.

= .

ENDIF.

ENDLOOP .

CHECK sy-subrc = 0.

INSERT INTO (tab_nam2) VALUES .

IF sy-subrc NE 0 .

UPDATE (tab_nam2) FROM .

ENDIF .

ADD 1 TO count_commit .

IF count_commit = '10000' .

COMMIT WORK .

count_commit = 0 .

ENDIF .

WRITE : .

ENDLOOP .

ENDFORM. " z_define_itab

&----


*& Form select_with_where

&----


FORM select_with_where.

IF NOT where1 IS INITIAL .

twhere-line = where1 . APPEND twhere .

ENDIF .

IF NOT where2 IS INITIAL .

twhere-line = where2 . APPEND twhere .

ENDIF .

IF NOT where3 IS INITIAL .

twhere-line = where3 . APPEND twhere .

ENDIF .

IF NOT where4 IS INITIAL .

twhere-line = where4 . APPEND twhere .

ENDIF .

REFRESH prog_tab.

append_line 'REPORT ZGEN .'.

append_line 'TABLES:'.

append_line tab_name.

append_line '.'.

append_line 'DATA: ITAB LIKE'.

append_line tab_name.

append_line 'OCCURS 0 WITH HEADER LINE.'.

append_line 'FORM SELECT_TABLE.'.

append_line 'SELECT * FROM'.

append_line tab_name.

IF pclient = 'X'.

append_line 'CLIENT SPECIFIED'.

ENDIF.

append_line 'INTO TABLE ITAB'.

append_line 'WHERE '.

LOOP AT twhere.

append_line twhere.

ENDLOOP.

append_line ' . '.

append_line 'ENDFORM.'.

PERFORM generate_form .

ENDFORM. " select_with_where

&----


*& Form generate_form

&----


FORM generate_form .

GENERATE SUBROUTINE POOL prog_tab NAME zprogram

MESSAGE zmessage LINE no_line .

IF sy-subrc NE 0.

WRITE: / 'Syntax error : ', zmessage,

/ 'in line', no_line .

STOP.

ENDIF.

SELECT * FROM (tab_name) INTO TABLE

WHERE (twhere) .

ENDFORM.

Also see this

8 REPLIES 8
Read only

Former Member
0 Likes
842

You can create dynamic internal tables using..

field-symbols: <table> type any.

types: fieldref type ref to data.

data: dyn_table type fieldref.

As for the actual code to generate the table:

create data dyn_table type (SAP Table).

assign dyn_table->* to table.

REPORT zselect_dynamic LINE-SIZE 132

LINE-COUNT 65(1)

NO STANDARD PAGE HEADING

MESSAGE-ID z1.

TYPES ztab LIKE dcobjdef-name .

PARAMETERS: tab_name TYPE ztab DEFAULT 'Z?????' ,

tab_nam2 TYPE ztab DEFAULT 'Z?????' ,

pclient AS CHECKBOX .

SELECTION-SCREEN SKIP .

PARAMETERS: where1(80) ,

where2(80) ,

where3(80) ,

where4(80) .

*

DATA : lcode(72),

prog_tab LIKE lcode OCCURS 0 WITH HEADER LINE .

DEFINE append_line.

append &1 to prog_tab.

END-OF-DEFINITION.

DATA: BEGIN OF nametab OCCURS 0.

INCLUDE STRUCTURE dntab.

DATA: END OF nametab.

DATA: BEGIN OF twhere OCCURS 20,

line(80),

END OF twhere.

DATA: zprogram LIKE sy-cprog,

no_line TYPE i,

zmessage(150) ,

count_commit TYPE i .

DATA: d_ref TYPE REF TO data,

d_ref2 TYPE REF TO data ,

lt_alv_cat TYPE TABLE OF lvc_s_fcat,

ls_alv_cat LIKE LINE OF lt_alv_cat.

FIELD-SYMBOLS : TYPE table,

,

,"TYPE ANY ,

,

.

**----


    • Main program.

**----


START-OF-SELECTION.

END-OF-SELECTION.

**----


    • Main program.

**----


PERFORM z_define_itab .

&----


*& Form z_define_itab

&----


FORM z_define_itab .

CHECK ( tab_name(01) = 'Z' OR tab_name(01) = 'Y' )

  • if you want treat tables with your namespace, insert here your code

AND ( tab_nam2(01) = 'Z' OR tab_nam2(01) = 'Y' ) .

REFRESH nametab.

CALL FUNCTION 'NAMETAB_GET'

EXPORTING

langu = sy-langu

tabname = tab_name

TABLES

nametab = nametab

EXCEPTIONS

no_texts_found = 1.

LOOP AT nametab .

ls_alv_cat-fieldname = nametab-fieldname .

ls_alv_cat-ref_table = tab_name.

ls_alv_cat-ref_field = nametab-fieldname .

APPEND ls_alv_cat TO lt_alv_cat.

ENDLOOP.

  • internal table build

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_alv_cat

IMPORTING ep_table = d_ref .

ASSIGN d_ref->* TO .

IF where1 IS INITIAL AND where2 IS INITIAL

AND where3 IS INITIAL AND where4 IS INITIAL .

SELECT * FROM (tab_name) INTO TABLE .

  • ORDER BY PRIMARY KEY.

ELSE .

PERFORM select_with_where .

ENDIF .

DESCRIBE TABLE LINES sy-tfill .

IF sy-tfill = 0 .

MESSAGE i000 WITH

'Data not selected, verify the tables or conditions ! ' .

STOP .

ELSE .

MESSAGE s000 WITH 'You have successfully treated yours tables.'.

ENDIF .

CREATE DATA d_ref2 TYPE (tab_nam2).

ASSIGN d_ref2->* TO .

LOOP AT ASSIGNING .

CLEAR .

LOOP AT nametab .

ASSIGN COMPONENT nametab-fieldname

OF STRUCTURE TO .

IF sy-subrc <> 0. EXIT. ENDIF.

ASSIGN COMPONENT nametab-fieldname OF STRUCTURE TO .

IF sy-subrc = 0.

= .

ENDIF.

ENDLOOP .

CHECK sy-subrc = 0.

INSERT INTO (tab_nam2) VALUES .

IF sy-subrc NE 0 .

UPDATE (tab_nam2) FROM .

ENDIF .

ADD 1 TO count_commit .

IF count_commit = '10000' .

COMMIT WORK .

count_commit = 0 .

ENDIF .

WRITE : .

ENDLOOP .

ENDFORM. " z_define_itab

&----


*& Form select_with_where

&----


FORM select_with_where.

IF NOT where1 IS INITIAL .

twhere-line = where1 . APPEND twhere .

ENDIF .

IF NOT where2 IS INITIAL .

twhere-line = where2 . APPEND twhere .

ENDIF .

IF NOT where3 IS INITIAL .

twhere-line = where3 . APPEND twhere .

ENDIF .

IF NOT where4 IS INITIAL .

twhere-line = where4 . APPEND twhere .

ENDIF .

REFRESH prog_tab.

append_line 'REPORT ZGEN .'.

append_line 'TABLES:'.

append_line tab_name.

append_line '.'.

append_line 'DATA: ITAB LIKE'.

append_line tab_name.

append_line 'OCCURS 0 WITH HEADER LINE.'.

append_line 'FORM SELECT_TABLE.'.

append_line 'SELECT * FROM'.

append_line tab_name.

IF pclient = 'X'.

append_line 'CLIENT SPECIFIED'.

ENDIF.

append_line 'INTO TABLE ITAB'.

append_line 'WHERE '.

LOOP AT twhere.

append_line twhere.

ENDLOOP.

append_line ' . '.

append_line 'ENDFORM.'.

PERFORM generate_form .

ENDFORM. " select_with_where

&----


*& Form generate_form

&----


FORM generate_form .

GENERATE SUBROUTINE POOL prog_tab NAME zprogram

MESSAGE zmessage LINE no_line .

IF sy-subrc NE 0.

WRITE: / 'Syntax error : ', zmessage,

/ 'in line', no_line .

STOP.

ENDIF.

SELECT * FROM (tab_name) INTO TABLE

WHERE (twhere) .

ENDFORM.

Also see this

Read only

Former Member
0 Likes
841

HI

Try this

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

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

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

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

Cheers:)

Mithlesh

Read only

Former Member
0 Likes
841

Hi,

You can create a simple dynamic table using this piece of code.

Code

data: i_table type ref to data.

field-symbols : <list> type table .

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = lt_alv_cat

importing

ep_table = i_table .

assign i_table->* to <list> .

Read only

Former Member
0 Likes
841

Hi Deepak,

Check this thread..

and search for some topics..

regards

vijay

Read only

Former Member
0 Likes
841

Hi

refer this link for simple example

http://www.sap-img.com/ab030.htm

regards

kishore

Read only

hymavathi_oruganti
Active Contributor
0 Likes
841

for creating dynmic int table follow the following steps.

example:

data: begin of itab occurs 0,

sno type i,

sname(10),

marks type i,

end of itab.

Now my requirement is like below, marks should become headings of alv grid.

we donno how many marks will come. so , the requirement is dynamic.

sno| sname| 80 | 90 | 78 | 77...........

build a LVC fieldcat like below.

ls_fieldcat-fieldname = 'SNO'.

ls_fieldcat-col_text = 'SNO'.

append ls_fieldcat to lt_fieldcat.

ls_fieldcat-fieldname = 'SNAME'.

ls_fieldcat-col_text = 'SNAME'.

append ls_fieldcat to lt_fieldcat.

*******for marks*********

loop at itab

ls_fieldcat-fieldname = itab-marks.

ls_fieldcat-col_text = itab-marks.

append ls_fieldcat to lt_fieldcat.

endloop.

**************

2. NOW THE FIELDCAT IS UILT. WE HAVE TO BUILD AN INTERNAL TABLE FROM THE FIELDCAT.

DATA: DREF TYPE REF TO DATA,WA_REF TYPE REF TO DATA.

FIELD-SYMBOLS: <TEMP_TAB> TYPE TABLE, <TEMP_WA> TYPE ANY.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = LT_LVCFIELDCAT

IMPORTING

EP_TABLE = DREF.

ASSIGN dref->* TO <TEMP_TAB>.

<temp_tab> is the dynamic internal table built from fieldcat.

3. NOW FILL THE DYNAMIC INTERNAL TABLE.

CREATE DATA WA_REF LIKE LINE OF <TEMP_TAB>.

ASSIGN WA_REF->* TO <TEMP_WA>.

LOOP AT ITAB.

MOVE-CORRESPONDING ITAB TO <TEMP_WA>.

APPEND <TEMP_WA> TO <TEMP_TAB>.

CLEAR IT_ITAB.

ENDLOOP.

ENDFORM. " FILL_DYN_TAB

Read only

Former Member
0 Likes
841

Hi

You can also Try this

TABLES: mara, makt.

TYPE-POOLS: slis.

DATA: it_fcat

TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat,

ls_layout TYPE slis_layout_alv.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF

it_fieldcat.

DATA: new_table TYPE REF TO data,

new_line TYPE REF TO data,

ob_cont_alv TYPE REF TO cl_gui_custom_container,

ob_alv TYPE REF TO cl_gui_alv_grid,

vg_campos(255) TYPE c,

i_campos LIKE TABLE OF vg_campos,

vg_campo(30) TYPE c,

vg_tables(60) TYPE c.

*DATA: e_params LIKE zutsvga_alv_01.

FIELD-SYMBOLS: <l_table> TYPE table,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

PARAMETERS: p_max(2) TYPE n DEFAULT '20' OBLIGATORY.

is_fcat-fieldname = 'COL01'.

is_fcat-ref_fieldname = 'MATNR'.

is_fcat-ref_tabname = 'MARA'.

APPEND is_fcat TO it_fcat.

is_fcat-fieldname = 'COL02'.

is_fcat-ref_fieldname = 'MAKTX'.

is_fcat-ref_tabname = 'MAKT'.

APPEND is_fcat TO it_fcat.

LOOP AT it_fcat INTO is_fcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-ref_fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

CONCATENATE is_fieldcat-ref_table is_fieldcat-ref_field INTO

vg_campos SEPARATED BY '~'.

APPEND vg_campos TO i_campos.

ENDLOOP.

*... Create the dynamic internal table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

*... Create a new line

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

SELECT (i_campos) FROM mara INNER JOIN makt ON maramatnr = maktmatnr

UP TO P_MAX ROWS INTO TABLE <l_table>.

describe table <l_table>.

write 😕 sy-tfill.

LOOP AT <l_table> INTO <l_line>.

LOOP AT it_fcat INTO is_fcat.

ASSIGN COMPONENT is_fcat-fieldname OF STRUCTURE <l_line> TO <l_field>.

IF sy-tabix = 1.

WRITE: /2 <l_field>.

ELSE.

WRITE: <l_field>.

ENDIF.

ENDLOOP.

ENDLOOP.

Cheers:)

Mithlesh

Read only

Former Member
0 Likes
841

Hi Deepak,

You can refer to :

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

and many more weblogs... just use the search button on the top left hand and there are many articles on Dynamic internal table.

Best Regards,

Subramanian V.