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: 

write dynamic table

Former Member
0 Kudos

hello experts,

please help me with this.

i have a dynamic table and my output should be in a report format (write:).

how can i WRITE the output using the dynamic table? and another thing, i should write the heading or the column names also...

for example:

Material Number----Material Type-----Material Description -


>column names

0000000012--


XXXX--


YYYYYY -


>values

thank you.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

we use Dynamic internal tables when we use ABAP oops

we create them by using Field symbols

There are a few declarations to make:

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.

The table is now a field symbol which can be referenced in the normal way.

check out this prg

REPORT ZCLUST1 .

Example: how to create a dynamic internal table

The dynamic internal table stucture


DATA: BEGIN OF STRUCT OCCURS 10, 
FILDNAME(8) TYPE C, 
ABPTYPE TYPE C, 
LENGTH TYPE I, 
END OF STRUCT. 


The dynamic program source table 
DATA: BEGIN OF INCTABL OCCURS 10, 
LINE(72), 
END OF INCTABL. 

DATA: LNG TYPE I, TYPESRTING(6). 


Sample dynamic internal table stucture 
STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'. 
APPEND STRUCT. CLEAR STRUCT. 

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'. 
APPEND STRUCT. CLEAR STRUCT. 

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'. 
APPEND STRUCT. CLEAR STRUCT. 


Create the dynamic internal table definition in the dyn. program 
INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL. 
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL. 

LOOP AT STRUCT. 
INCTABL-LINE = STRUCT-FILDNAME. 
LNG = STRLEN( STRUCT-FILDNAME ). 

IF NOT STRUCT-LENGTH IS INITIAL . 
TYPESRTING(1) = '('. 
TYPESRTING+1 = STRUCT-LENGTH. 
TYPESRTING+5 = ')'. 
CONDENSE TYPESRTING NO-GAPS. 
INCTABL-LINE+LNG = TYPESRTING. 
ENDIF. 

INCTABL-LINE+15 = 'type '. 
INCTABL-LINE+21 = STRUCT-ABPTYPE. 
INCTABL-LINE+22 = ','. 
APPEND INCTABL. 
ENDLOOP. 
INCTABL-LINE = 'end of dyntab. '. 
APPEND INCTABL. 


Create the code processes the dynamic internal table 
INCTABL-LINE = ' '. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL. 
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL. 
INCTABL-LINE = ' '. APPEND INCTABL. 
INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL. 
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL. 
INCTABL-LINE = 'endloop.'. APPEND INCTABL. 


Create and run the dynamic program 
INSERT REPORT 'zdynpro'(001) FROM INCTABL. 
SUBMIT ZDYNPRO.
--------------------------------------------------------------------------------
 
or Just try out this simpler dynamic internal tables 

DATA: itab TYPE STANDARD TABLE OF spfli, 
wa LIKE LINE OF itab. 

DATA: line(72) TYPE c, 
list LIKE TABLE OF line(72). 

START-OF-SELECTION. 
*line = ' CITYFROM CITYTO '. 
line = ' AIRPTO '. 
APPEND line TO list. 

SELECT DISTINCT (list) 
INTO CORRESPONDING FIELDS OF TABLE itab 
FROM spfli. 

IF sy-subrc EQ 0. 
LOOP AT itab INTO wa. 

WRITE: / wa-cityfrom, wa-cityto. 
WRITE 😕 wa-airpto. 
ENDLOOP. 
ENDIF. 

for more info visit

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

Regards

Chandru

5 REPLIES 5

Former Member
0 Kudos

we use Dynamic internal tables when we use ABAP oops

we create them by using Field symbols

There are a few declarations to make:

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.

The table is now a field symbol which can be referenced in the normal way.

check out this prg

REPORT ZCLUST1 .

Example: how to create a dynamic internal table

The dynamic internal table stucture


DATA: BEGIN OF STRUCT OCCURS 10, 
FILDNAME(8) TYPE C, 
ABPTYPE TYPE C, 
LENGTH TYPE I, 
END OF STRUCT. 


The dynamic program source table 
DATA: BEGIN OF INCTABL OCCURS 10, 
LINE(72), 
END OF INCTABL. 

DATA: LNG TYPE I, TYPESRTING(6). 


Sample dynamic internal table stucture 
STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'. 
APPEND STRUCT. CLEAR STRUCT. 

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'. 
APPEND STRUCT. CLEAR STRUCT. 

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'. 
APPEND STRUCT. CLEAR STRUCT. 


Create the dynamic internal table definition in the dyn. program 
INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL. 
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL. 

LOOP AT STRUCT. 
INCTABL-LINE = STRUCT-FILDNAME. 
LNG = STRLEN( STRUCT-FILDNAME ). 

IF NOT STRUCT-LENGTH IS INITIAL . 
TYPESRTING(1) = '('. 
TYPESRTING+1 = STRUCT-LENGTH. 
TYPESRTING+5 = ')'. 
CONDENSE TYPESRTING NO-GAPS. 
INCTABL-LINE+LNG = TYPESRTING. 
ENDIF. 

INCTABL-LINE+15 = 'type '. 
INCTABL-LINE+21 = STRUCT-ABPTYPE. 
INCTABL-LINE+22 = ','. 
APPEND INCTABL. 
ENDLOOP. 
INCTABL-LINE = 'end of dyntab. '. 
APPEND INCTABL. 


Create the code processes the dynamic internal table 
INCTABL-LINE = ' '. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL. 
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL. 
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL. 
INCTABL-LINE = ' '. APPEND INCTABL. 
INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL. 
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL. 
INCTABL-LINE = 'endloop.'. APPEND INCTABL. 


Create and run the dynamic program 
INSERT REPORT 'zdynpro'(001) FROM INCTABL. 
SUBMIT ZDYNPRO.
--------------------------------------------------------------------------------
 
or Just try out this simpler dynamic internal tables 

DATA: itab TYPE STANDARD TABLE OF spfli, 
wa LIKE LINE OF itab. 

DATA: line(72) TYPE c, 
list LIKE TABLE OF line(72). 

START-OF-SELECTION. 
*line = ' CITYFROM CITYTO '. 
line = ' AIRPTO '. 
APPEND line TO list. 

SELECT DISTINCT (list) 
INTO CORRESPONDING FIELDS OF TABLE itab 
FROM spfli. 

IF sy-subrc EQ 0. 
LOOP AT itab INTO wa. 

WRITE: / wa-cityfrom, wa-cityto. 
WRITE 😕 wa-airpto. 
ENDLOOP. 
ENDIF. 

for more info visit

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

Regards

Chandru

Former Member
0 Kudos

Hi,

Check out the below code


DATA: lw_fieldvalue(40) TYPE c,
lw_fieldname(30) TYPE c.

gwa_output TYPE REF TO data.

FIELD-SYMBOLS: <gwa_output> TYPE ANY.

FIELD-SYMBOLS: <l_fvalue> TYPE ANY.


*---Create a pointer to store the workarea of the final output table.
CREATE DATA gwa_output LIKE LINE OF <git_output>.(this is the output table)


*---Assign it to a fieldsymbol which will be used in populating the
*---final output table.
ASSIGN gwa_output->* TO <gwa_output>.


Loop at i_tab into wa_tab.

move wa_tab-field1 to v_fieldvalue. (moving the first field value into field value variable).

CONDENSE v_fieldvalue NO-GAPS. (Condense the output).

CLEAR: <gwa_output>.
ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <gwa_output> TO <l_fvalue>.
<l_fvalue> = V_fieldvalue.



Repeat the same with other fields



*---Append the current record to final output table.
APPEND <gwa_output> TO <git_output>.

endloop.

You need to assign the component of table <dyn_Tab> to field symbol and than give value to that field symbol.

Like:

ASSIGN COMPONENT FLD1 OF STRUCTURE <DYN_TAB> TO <FS>.
<FS> = I_TAB-FLD1. 

Try this code, it generates field names dynamically





Example: how to create a dynamic internal table 
*

The dynamic internal table stucture 
DATA: BEGIN OF struct OCCURS 10,
fildname(8) TYPE c,
abptype TYPE c,
length TYPE i,
END OF struct.


The dynamic program source table 
DATA: BEGIN OF inctabl OCCURS 10,
line(72),
END OF inctabl.

DATA: lng TYPE i, typesrting(6).


Sample dynamic internal table stucture 
struct-fildname = 'field1'. struct-abptype = 'c'. struct-length = '6'.
APPEND struct. CLEAR struct.

struct-fildname = 'field2'. struct-abptype = 'd'.
APPEND struct. CLEAR struct.

struct-fildname = 'field3'. struct-abptype = 'i'.
APPEND struct. CLEAR struct.


Create the dynamic internal table definition in the dyn. program 
inctabl-line = 'program zdynpro.'. APPEND inctabl.
inctabl-line = 'data: begin of dyntab occurs 10,'. APPEND inctabl.

LOOP AT struct.
inctabl-line = struct-fildname.
lng = STRLEN( struct-fildname ).

IF NOT struct-length IS INITIAL .
typesrting(1) = '('.
typesrting+1 = struct-length.
typesrting+5 = ')'.
CONDENSE typesrting NO-GAPS.
inctabl-line+lng = typesrting.
ENDIF.

inctabl-line+15 = 'type '.
inctabl-line+21 = struct-abptype.
inctabl-line+22 = ','.
APPEND inctabl.
ENDLOOP.
inctabl-line = 'end of dyntab. '.
APPEND inctabl.


Create the code processes the dynamic internal table 
inctabl-line = ' '. APPEND inctabl.
inctabl-line = 'dyntab-field1 = ''aaaaaa''.'. APPEND inctabl.
inctabl-line = 'dyntab-field2 = ''19970814''.'. APPEND inctabl.
inctabl-line = 'dyntab-field3 = 1.'. APPEND inctabl.
inctabl-line = 'append dyntab.'. APPEND inctabl.
inctabl-line = ' '. APPEND inctabl.
inctabl-line = 'loop at dyntab.'. APPEND inctabl.
inctabl-line = 'write: / dyntab-field1, dyntab-field2, dyntab-field3.'.
APPEND inctabl.
inctabl-line = 'endloop.'. APPEND inctabl.


Create and run the dynamic program 
INSERT REPORT 'ZDYNPRO' FROM inctabl.
IF sy-subrc = 0.
SUBMIT zdynpro.

ELSE.
WRITE : / 'error'.
ENDIF.

Regards,

Chandru

Former Member
0 Kudos

hello,

i already created the dynamic table and populated the values. i just have to have a report output using WRITE statement..

how can i WRITE the output using the dynamic table?

thanks..:-)

Former Member
0 Kudos

hello,

just a clarification, the fieldnames should not be like MATNR of MAKTX. it should be like: Material number (for matnr), Material description...i believe it's in the field SELTEXT or column identifier of structure LVC_S_FCAT.

can i also write that one?before i write the values?

thanks.

0 Kudos

Hi,

To Output material Number instead of MATNR then you must use SELECTION TEXT.

For this you need to

1. go to the Goto tab on the menu bar select Selection Text

2. If you have already activated the program then all the fields will be available there if not then enter manually and

3. If you want to display the names available for the fields in the dictionary then tick the checkbox in the last column

4. if you want to display your own name for that field enter the name manaully without selecting the checkbox.

Regards,

Chandru