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: 

Dynamic Structure

Former Member
0 Kudos
83

Hi ,

How can i define dynamic structure in my program i.e. there is a ztable in which some indefinite no of enteries to be maintained.

Now I need to display no columns in my ALV report .

The no of columns should be equal to no of enteries in ztable.

How to go about it.

Kindly suggest

1 ACCEPTED SOLUTION

Former Member
0 Kudos
56

Hi Supriya ,

Welcome to SCN.

You can achieve it with RTTC concept.

Check my tutorials in the given links for detrails concept.

detail code is:

REPORT Z_RTTC.
PARAMETERS:
p_tab TYPE dd02l-tabname .                  " Input data table name
 
FIELD-SYMBOLS:
<fsym_itab> TYPE ANY TABLE,                 " Field symbol for internal table
<fsym_warea> TYPE ANY,                      " Field symbol for work area
<fsym_field> TYPE ANY.                      " Field symbol for field value

DATA:
ref_rowtype
TYPE REF TO cl_abap_structdescr,            " Rowtype ref to RTTS
ref_tabletype
TYPE REF TO cl_abap_tabledescr.             " Internal table type ref to RTTS

DATA:
ref_wa TYPE REF TO data, 		       " Declaration of work area instance
ref_itab TYPE REF TO data.                  " Declaration of internal table instance
 
 
*"---------------------------------------------------------------------
* START-OF-SELECTION
*"---------------------------------------------------------------------

START-OF-SELECTION.

* Create row type and tabletype by RTTC technique
ref_rowtype ?= cl_abap_typedescr=>describe_by_name( p_tab ).
ref_tabletype = cl_abap_tabledescr=>create( p_line_type = ref_rowtype).
 
* Object type created by RTTC.
CREATE DATA ref_itab TYPE HANDLE ref_tabletype.
CREATE DATA ref_wa TYPE HANDLE ref_rowtype.
 
*Type assignment
ASSIGN ref_itab->* TO <fsym_itab>.
ASSIGN ref_wa->* TO <fsym_warea>.
 
* Value assignment to the internal table 
SELECT *
FROM (p_tab)
INTO TABLE <fsym_itab>.
 
* Display Output 
LOOP AT <fsym_itab> INTO <fsym_warea>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <fsym_warea> TO

<fsym_field>.

IF sy-subrc NE 0.

NEW-LINE.                           " Shift Cursor to next line
EXIT.                               " EXIT to next work area

ENDIF.                              " IF sy-subrc NE 0.

WRITE <fsym_field>.

ENDDO.

ENDLOOP.                            " LOOP AT <fsym_itab> INTO <fsym_warea>.

in the parameter fileld you have to put your ztable name and you will get the data stored in it in the list..

[Detail concept|https://wiki.sdn.sap.com/wiki/x/_oDVAg ]

[Dynamic creation of ALV|https://wiki.sdn.sap.com/wiki/x/iADDAw ]

Regards,

Anirban

2 REPLIES 2

Former Member
0 Kudos
57

Hi Supriya ,

Welcome to SCN.

You can achieve it with RTTC concept.

Check my tutorials in the given links for detrails concept.

detail code is:

REPORT Z_RTTC.
PARAMETERS:
p_tab TYPE dd02l-tabname .                  " Input data table name
 
FIELD-SYMBOLS:
<fsym_itab> TYPE ANY TABLE,                 " Field symbol for internal table
<fsym_warea> TYPE ANY,                      " Field symbol for work area
<fsym_field> TYPE ANY.                      " Field symbol for field value

DATA:
ref_rowtype
TYPE REF TO cl_abap_structdescr,            " Rowtype ref to RTTS
ref_tabletype
TYPE REF TO cl_abap_tabledescr.             " Internal table type ref to RTTS

DATA:
ref_wa TYPE REF TO data, 		       " Declaration of work area instance
ref_itab TYPE REF TO data.                  " Declaration of internal table instance
 
 
*"---------------------------------------------------------------------
* START-OF-SELECTION
*"---------------------------------------------------------------------

START-OF-SELECTION.

* Create row type and tabletype by RTTC technique
ref_rowtype ?= cl_abap_typedescr=>describe_by_name( p_tab ).
ref_tabletype = cl_abap_tabledescr=>create( p_line_type = ref_rowtype).
 
* Object type created by RTTC.
CREATE DATA ref_itab TYPE HANDLE ref_tabletype.
CREATE DATA ref_wa TYPE HANDLE ref_rowtype.
 
*Type assignment
ASSIGN ref_itab->* TO <fsym_itab>.
ASSIGN ref_wa->* TO <fsym_warea>.
 
* Value assignment to the internal table 
SELECT *
FROM (p_tab)
INTO TABLE <fsym_itab>.
 
* Display Output 
LOOP AT <fsym_itab> INTO <fsym_warea>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <fsym_warea> TO

<fsym_field>.

IF sy-subrc NE 0.

NEW-LINE.                           " Shift Cursor to next line
EXIT.                               " EXIT to next work area

ENDIF.                              " IF sy-subrc NE 0.

WRITE <fsym_field>.

ENDDO.

ENDLOOP.                            " LOOP AT <fsym_itab> INTO <fsym_warea>.

in the parameter fileld you have to put your ztable name and you will get the data stored in it in the list..

[Detail concept|https://wiki.sdn.sap.com/wiki/x/_oDVAg ]

[Dynamic creation of ALV|https://wiki.sdn.sap.com/wiki/x/iADDAw ]

Regards,

Anirban

Former Member
0 Kudos
56

Hi Anirban ,

Thnks for your valuable reply.

The answer does not fully solved my problem but I got the direction to proceed further.

Supriya