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: 

How dynamically change internal table structure by 'Create data'

com_2018
Participant
0 Kudos

Dear experts,

How dynamically change internal table structure by 'Create data'?

Example:

before sturcture

ITAB field1 field2

after structure

ITAB field1 field2 field3 field4 .......

Please give me any idea.

Thanks and regards,

collysun.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

You can use mthod create_dynamic_table of class cl_alv_table_create.

Checkout this weblog -

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

Cheers.

Sanjay

3 REPLIES 3

Former Member
0 Kudos

Plz reward points if it helps you

if your structure changes from one defined structure to another one, you can try this:

ASSIGN yourfield CASTING TYPE yourstructure

or CREATE DATA yourdata TYPE yourstructure.

If your structure has to be really dynamic - like ALV - you can use the class CL_ALV_TABLE_CREATE.

try this example:

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

REPORT zxxx_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' "any table name (sap or ztable)

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

0 Kudos

HI,

This is OLE program which i created , using dynamic table concept,

just copy past the code and run it in se38.

give table name as mara,or knvv or any thing z-table name

dont change other parameter.

you will see the inernal table is created dynamically, first column names are retrieved dynamically and the 5 row of data of that table is in output.hope you will like it.

REPORT ztest_table MESSAGE-ID zz.

TYPE-POOLS: slis.

INCLUDE ole2incl.

*

PARAMETERS: p_table LIKE dd02l-tabname.

PARAMETER: p_col1 type i default 1,

p_col2 type i default 5 .

*--


Data for table definition--


DATA: BEGIN OF tab OCCURS 500,

fieldname LIKE dd03l-fieldname,

END OF tab.

*--


data for OLE--


DATA: gs_word TYPE ole2_object,

gs_documents TYPE ole2_object,

gs_actdoc TYPE ole2_object,

gs_application TYPE ole2_object,

gs_options TYPE ole2_object,

gs_actwin TYPE ole2_object,

gs_actpan TYPE ole2_object,

gs_view TYPE ole2_object,

gs_selection TYPE ole2_object,

gs_font TYPE ole2_object,

gs_parformat TYPE ole2_object,

gs_tables TYPE ole2_object,

gs_range TYPE ole2_object,

gs_table TYPE ole2_object,

gs_table_border TYPE ole2_object,

gs_cell TYPE ole2_object,

gs_paragraph TYPE ole2_object.

DATA: gv_pos(5) TYPE n.

data: str(50).

*

at selection-screen.

perform validate_table_name.

perform validate_col_no.

START-OF-SELECTION.

PERFORM create_ole_word.

PERFORM table_draw.

----


Form Definition -

&----


*& Form create_ole_word

&----


FORM create_ole_word.

CREATE OBJECT gs_word 'word.application'.

IF sy-subrc <> 0 .

MESSAGE e303 WITH 'Error in creating ole object'.

ENDIF.

SET PROPERTY OF gs_word 'Visible' = 1.

GET PROPERTY OF gs_word 'Documents' = gs_documents.

CALL METHOD OF gs_documents 'Add'.

GET PROPERTY OF gs_word 'ActiveDocument' = gs_actdoc.

GET PROPERTY OF gs_actdoc 'Application' = gs_application .

GET PROPERTY OF gs_application 'Options' = gs_options .

SET PROPERTY OF gs_options 'MeasurementUnit' = '1' .

GET PROPERTY OF gs_application 'Selection' = gs_selection .

GET PROPERTY OF gs_selection 'Font' = gs_font .

GET PROPERTY OF gs_selection 'ParagraphFormat' = gs_parformat .

SET PROPERTY OF gs_font 'Bold' = '1' . "Not bold

SET PROPERTY OF gs_font 'Italic' = '0' . "Italic

SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined

SET PROPERTY OF gs_parformat 'Alignment' = '1' .

CALL METHOD OF gs_selection 'TypeText'

EXPORTING #1 = 'This is an OLE example!'.

CALL METHOD OF gs_selection 'TypeParagraph' .

  • SET PROPERTY OF gs_view 'SeekView' = '0' .

*--Reseting font attributes for the title

SET PROPERTY OF gs_font 'Name' = 'Times New Roman' .

SET PROPERTY OF gs_font 'Size' = '10' .

SET PROPERTY OF gs_font 'Bold' = '1' . "Bold

SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic

SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF gs_parformat 'Alignment' = '1' . "Centered

CALL METHOD OF gs_selection 'TypeText'

EXPORTING

#1 = text-000.

*--Advancing cursor to the new line

CALL METHOD OF gs_selection 'TypeParagraph' .

*--reseting font attributes for ordinary text

SET PROPERTY OF gs_font 'Name' = 'Times New Roman' .

SET PROPERTY OF gs_font 'Size' = '10' .

SET PROPERTY OF gs_font 'Bold' = '0' . "Not bold

SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic

SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined

concatenate 'Table Data of :' p_table into str separated by space.

*--Setting paragraph format attribute

SET PROPERTY OF gs_parformat 'Alignment' = '1' . "Justified

CALL METHOD OF gs_selection 'TypeText'

EXPORTING

#1 = str.

*--Skip some lines

DO 4 TIMES .

CALL METHOD OF gs_selection 'TypeParagraph' .

ENDDO .

*--Getting entity handles for the entities on the way

GET PROPERTY OF gs_actdoc 'Tables' = gs_tables .

GET PROPERTY OF gs_selection 'Range' = gs_range .

ENDFORM. " create_ole_word

&----


*& Form table_draw

&----


FORM table_draw.

DATA: column_count TYPE i,

row_count TYPE i,

t_col TYPE i VALUE 1,

t_row TYPE i VALUE 1.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

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.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

*----Creating dynamic structure--


CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = p_table

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat.

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

*----


getting field name -


t_col = 1.

SELECT fieldname INTO tab FROM dd03l WHERE tabname EQ p_table.

if TAB-FIELDNAME NP 'INCLUDE'.

if SY-DBCNT >= p_col1 and

SY-DBCNT <= p_col2.

append tab.

t_col = t_col + 1.

endif.

else.

sy-dbcnt = sy-dbcnt - 1.

endif.

if t_col > 5.

exit.

endif.

endselect.

DESCRIBE TABLE tab LINES column_count.

CALL METHOD OF gs_tables 'Add' = gs_table

EXPORTING

#1 = gs_range " Handle for range entity

#2 = '11' "Number of rows

#3 = column_count. "Number of columns

*--Setting border attribute for the table

GET PROPERTY OF gs_table 'Borders' = gs_table_border.

SET PROPERTY OF gs_table_border 'Enable' = '1' . "With

*----


header of table

t_col = 1.

LOOP AT tab.

*--Getting cell coordinates

CALL METHOD OF gs_table 'Cell' = gs_cell

EXPORTING

#1 = '1' "first row

#2 = t_col. "first column

**--Getting the range handle to write the text

GET PROPERTY OF gs_cell 'Range' = gs_range .

*--Filling the cell

SET PROPERTY OF gs_range 'Text' = tab-fieldname .

t_col = t_col + 1.

ENDLOOP.

t_row = t_row + 1.

*--


filling data--


SELECT (TAB) FROM (p_table) INTO TABLE <l_table> up to 10 rows.

LOOP AT <l_table> ASSIGNING <l_line>.

t_col = 1.

LOOP AT TAB .

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

CALL METHOD OF gs_table 'Cell' = gs_cell

EXPORTING

#1 = t_row

#2 = t_col. "second column

GET PROPERTY OF gs_cell 'Range' = gs_range .

SET PROPERTY OF gs_range 'Text' = <l_field> .

t_col = t_col + 1.

ENDLOOP.

t_row = t_row + 1.

if t_row > 11.

exit.

endif.

ENDLOOP.

FREE OBJECT gs_word .

ENDFORM. " table_draw

&----


*& Form validate_table_name

&----


form validate_table_name.

SELECT fieldname FROM dd03l into corresponding fields

of table tab where tabname = p_table.

if sy-subrc <> 0.

message e307 with 'Table does not exist'.

endif.

clear tab[].

endform. " validate_table_name

&----


*& Form validate_col_no

&----


form validate_col_no.

data: lv_diff type i.

if p_col1 < 1 or p_col2 < 1.

message e307 with 'Wrong Column Number'.

elseif p_col2 < p_col2.

message e307 with 'Final Column No should be greater than initial'.

endif.

lv_diff = p_col2 - p_col1.

if lv_diff >= 5.

message e307 with 'Maximum 5 Column can be printed'.

endif.

endform. " validate_col_no

Former Member
0 Kudos

You can use mthod create_dynamic_table of class cl_alv_table_create.

Checkout this weblog -

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

Cheers.

Sanjay