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 table

Former Member
0 Likes
759

how to create dynamic internal table? suppose client want 3 column out of x column in my database table , how will i make it dynamic .if he enters 3 , 3 column should be created ,if enters 5 ,will create 5 column? do i have to make structure and by using field symbols ? please let me know.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
718

Hi mayank,

1.

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are

LIST OF FIELDS, (just as u require)

and from those, it consructs dynamic table.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

3. U can use this program (FORM in this program)

to generate any kind of internal table

by specifying list of fields.

4.

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

*----


START-OF-SELECTION.

*----


field list

ddfields-fieldname = 'BUKRS'.

APPEND DDFIELDS.

ddfields-fieldname = 'MATNR'.

APPEND DDFIELDS.

*----


PERFORM mydyntable .

*----


see <DYNTABLE> in debug mode.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable .

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

data : lt TYPE lvc_t_fcat .

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

regards,

amit m.

6 REPLIES 6
Read only

Former Member
0 Likes
718

hi mayank,

try this test report for dynamic internal table,

&----


*& Report ZTESTPAV5

*&

&----


*&

*&

&----


REPORT ZTESTPAV5.

TYPE-POOLS: slis.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

<dyn_wa>.

DATA: alv_fldcat TYPE slis_t_fieldcat_alv,

it_fldcat TYPE lvc_t_fcat.

DATA: lv_monate TYPE f,

lv_months TYPE i,

lv_date TYPE sy-datum.

lv_date = sy-datum + 360.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS: p_check TYPE c.

SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'

EXPORTING

i_datum_bis = lv_date

i_datum_von = sy-datum

i_kz_incl_bis = ' '

IMPORTING

e_monate = lv_monate.

lv_months = lv_monate.

PERFORM build_dyn_itab.

PERFORM build_report.

LOOP AT <dyn_table> INTO <dyn_wa>.

WRITE:/ <dyn_wa>.

ENDLOOP.

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

FORM build_dyn_itab.

DATA: index(3) TYPE c.

DATA: new_table TYPE REF TO data,

new_line TYPE REF TO data,

wa_it_fldcat TYPE lvc_s_fcat.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = 'AUFNR'.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 12.

APPEND wa_it_fldcat TO it_fldcat .

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = 'POSNR'.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 6.

APPEND wa_it_fldcat TO it_fldcat .

  • Create fields

CLEAR index.

DO 2 TIMES.

index = sy-index.

CLEAR wa_it_fldcat.

CONCATENATE 'Field' index INTO

wa_it_fldcat-fieldname .

CONDENSE wa_it_fldcat-fieldname NO-GAPS.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = new_table.

ASSIGN new_table->* TO <dyn_table>.

  • Create dynamic work area and assign to FS

CREATE DATA new_line LIKE LINE OF <dyn_table>.

ASSIGN new_line->* TO <dyn_wa>.

ENDFORM. "build_dyn_itab

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

FORM build_report.

DATA: fieldname(20) TYPE c.

DATA: fieldvalue(5) TYPE c.

DATA: index(3) TYPE c.

FIELD-SYMBOLS: <fs1>.

ASSIGN COMPONENT 'AUFNR' OF STRUCTURE <dyn_wa> TO <fs1>.

<fs1> = '123456789'.

ASSIGN COMPONENT 'POSNR' OF STRUCTURE <dyn_wa> TO <fs1>.

<fs1> = '000001'.

DO 2 TIMES.

index = sy-index.

  • Set up fieldname

CONCATENATE 'FIELD' index INTO fieldname .

CONDENSE fieldname NO-GAPS.

  • Set up fieldvalue

CONCATENATE 'FLD' index INTO fieldvalue.

CONDENSE fieldvalue NO-GAPS.

ASSIGN COMPONENT fieldname OF STRUCTURE <dyn_wa> TO <fs1>.

<fs1> = fieldvalue.

ENDDO.

  • Append to the dynamic internal table

APPEND <dyn_wa> TO <dyn_table>.

ENDFORM. "build_report

regards..

seshu.

Read only

Former Member
0 Likes
719

Hi mayank,

1.

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are

LIST OF FIELDS, (just as u require)

and from those, it consructs dynamic table.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

3. U can use this program (FORM in this program)

to generate any kind of internal table

by specifying list of fields.

4.

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

*----


START-OF-SELECTION.

*----


field list

ddfields-fieldname = 'BUKRS'.

APPEND DDFIELDS.

ddfields-fieldname = 'MATNR'.

APPEND DDFIELDS.

*----


PERFORM mydyntable .

*----


see <DYNTABLE> in debug mode.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable .

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

data : lt TYPE lvc_t_fcat .

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

regards,

amit m.

Read only

0 Likes
718

yaa i got ur answer , but again in this we are specifying column name , so again at one point it becomes static ,becoz in this prog you have hard coded colum names like BUKRS , MATNR .how will it be dynamic.

Read only

0 Likes
718

Hi again,

1. 3 column out of x column in my database table

What does that exactly mean?

2. In any case, we have to specify the column names,

either statically,

or by some logic at run time.

3. We can specify them at run time also in my code,

but still we have to know the field names atleast.

regards,

amit m.

Read only

0 Likes
718

ya thats true that we need to know fields name , how will you make it dynamic in your code.

Read only

0 Likes
718

hii amit,

please let me know for displaying that fields what should i do..see we have made two column BUKRS ,MATNR , now to see internal table with this two column what should i do.