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

how to create dynamic internal table.......

Former Member
0 Likes
1,094

Hi,

I have one field p_tabname which has got the value KNA1.

I have 5 fields in my program i.e. F1 F1 F3 F4 F5 Their values are KUNNR, LAND1, ORT01, NAME1

and TELF1. The values for the fields p_tabname and F1 F2 F3 F4 F5 come from selection screen.

Now i want to create an internal table with these 5 fields .

How can i achieve it?

Help will be appreciated.

Thanks,

Ibrahim

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,062
9 REPLIES 9
Read only

Former Member
0 Likes
1,063
Read only

Former Member
0 Likes
1,062

Hi,

You can search the previous threads on dynamic internal tables.

Thanx,

Pritha.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,062

Hi,

Please check out his code and adapt:

type-pools:slis.

*----


DATA DECLARATION

field-symbols: <dyntable> type standard table.

field-symbols: <dynline> type any.

field-symbols: <fld> type any.

field-symbols: <fs_data> type ref to data.

field-symbols: <fs_1>.

data: i_fieldlist type lvc_t_fcat,

wa_fieldlist type lvc_s_fcat,

v_iname like dd02l-tabname,

fldname(50) type c,

c(6) type c,

i_table_fieldlist like ddfield occurs 0 with header line,

i_data type ref to data,

lv_number_of_recs type i value 10,

lv_char(6) type c.

*----


SELECTION SCREEN

parameters : p_table(25) type c obligatory default 'MARA',

num_flds type n length 6 default 1,

p_recs type n length 6 default 10.

*----


start-of-selection.

v_iname = p_table.

*----


*STEP 1 : Creating the field list .i.e. specifying all fields that

  • will be displayed in the report.

*----


  • Getting all fields of the specified table in internal table

call function 'DD_NAMETAB_TO_DDFIELDS'

exporting

tabname = v_iname

tables

ddfields = i_table_fieldlist.

"CONSTRUCT FIELD LIST

  • Adding all additional fields required

loop at i_table_fieldlist.

wa_fieldlist-fieldname = i_table_fieldlist-fieldname.

append wa_fieldlist to i_fieldlist.

endloop.

  • ADDING CUSTOM FIELDS TO FIELD CATALOG

  • HERE WE ARE ADDING FIELDS OF TYPE C LENGHT 1

if num_flds > 0.

do num_flds times.

c = sy-index.

clear wa_fieldlist-fieldname.

concatenate 'FIELD' c into wa_fieldlist-fieldname.

condense wa_fieldlist-fieldname no-gaps.

append wa_fieldlist to i_fieldlist.

enddo.

endif.

*----


*STEP 2 : Generating an empty internal table (i_data) with all fields in our

  • field list table. This table will be filled with data further

  • and used for the report display

*----


assign i_data to <fs_data>.

*CREATING INTERNAL TABLE TO DISPLAY ALV DATA

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = i_fieldlist

importing

ep_table = <fs_data>

exceptions

generate_subpool_dir_full = 1

others = 2.

if sy-subrc <> 0.

message i023(zxxxfi) with 'Error while creating table'.

stop.

endif.

*----


*STEP 3 : Creating Dynamic field catalog which will be used to display report

  • NOTE: The field list table created above cannnot be used

  • as it doesnot have same structure as required in FM

  • REUSE_ALV_GRID_DISPLAY

*----


*CREATING FIELDCATALOG FOR PASSING TO REUSE_ALV_GRID_DISPLAY

data:i_fieldcat type slis_t_fieldcat_alv,

wa_fieldcat type slis_fieldcat_alv,

lv_field_num type i,

lv_var(6) type c,

lv_title type lvc_title.

loop at i_fieldlist into wa_fieldlist.

move-corresponding wa_fieldlist to wa_fieldcat.

wa_fieldcat-seltext_s = wa_fieldcat-fieldname.

wa_fieldcat-seltext_l = wa_fieldcat-fieldname.

wa_fieldcat-seltext_m = wa_fieldcat-fieldname.

append wa_fieldcat to i_fieldcat.

endloop.

describe table i_fieldcat lines lv_field_num.

lv_var = lv_field_num.

*----


*STEP 4 : SETTING GRID TITLE

*----


concatenate 'TABLE WITH '

lv_var

'FIELDS'

into lv_title

separated by space.

*----


*STEP 5 : Filling of internal table

*----


  • FILLING INTERNAL TABLE

assign <fs_data>->* to <fs_1>.

assign <fs_1> to <dyntable>.

field-symbols:<dyn_wa>,

<dyn_field>.

data:dy_line type ref to data.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyntable>. " creating a line type of the table just created above

assign dy_line->* to <dyn_wa>. " creating the work area with reference to the line type

if p_recs is not initial.

lv_number_of_recs = p_recs.

endif.

  • All fields are stored in table i_fieldcat as rows

  • All fields are stored in table <dyntable> as columns

  • Hence we have to loop at i_fieldcat to get each field where sy-tabix stores the

  • corresponding column number of the field to be filled in the work-area <dyn_wa>.

  • then we have to fill each column/field in the work area in the loop

do lv_number_of_recs times.

lv_char = sy-index.

loop at i_fieldcat into wa_fieldcat. " loop at fieldcat to get all fields

assign component sy-tabix "accessing corresponding field in the field catalog

of structure <dyn_wa> to <dyn_field>. "and assigning this field to a field symbol

if sy-subrc = 0.

<dyn_field> = lv_char. " filling value for this field

endif.

endloop.

append <dyn_wa> to <dyntable>.

enddo.

*----


*STEP 6 : Display report

*----


type-pools: slis.

data:wa_layout type slis_layout_alv.

*wa_layout-window_titlebar = 'Title'. "Overwrites title in title bar

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_grid_title = lv_title

it_fieldcat = i_fieldcat

i_default = ' '

is_layout = wa_layout

i_save = 'A'

tables

t_outtab = <dyntable>

exceptions

program_error = 1

others = 2.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

stop.

endif.

regards.

Read only

Former Member
0 Likes
1,062

Hi ,

To create the table dynamically you need to go SHDB ( T code for recording ) and record the table creation process and create a program from that recording and replace the table name and field vales etc from the parameters value ..

Regards

Pinaki

Read only

Former Member
0 Likes
1,062

Hi

try this sample:

PARAMETERS: P_TAB   TYPE DD02L-TABNAME.
PARAMETERS: FIELD1  TYPE LVC_FNAME,
            FIELD2  TYPE LVC_FNAME,
            FIELD3  TYPE LVC_FNAME,
            FIELD4  TYPE LVC_FNAME,
            FIELD5  TYPE LVC_FNAME.

DATA: T_CATALOG TYPE  LVC_T_FCAT.

DATA: MY_TABLE TYPE REF TO DATA,
      MY_LINE  TYPE REF TO DATA.

FIELD-SYMBOLS: <DYN_TABLE> TYPE TABLE,
               <WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
     EXPORTING
          I_STRUCTURE_NAME       = P_TAB
     CHANGING
          CT_FIELDCAT            = T_CATALOG
     EXCEPTIONS
          INCONSISTENT_INTERFACE = 1
          PROGRAM_ERROR          = 2
          OTHERS                 = 3.

IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

LOOP AT T_CATALOG TRANSPORTING NO FIELDS
                  WHERE FIELDNAME <> FIELD1 AND
                        FIELDNAME <> FIELD2 AND
                        FIELDNAME <> FIELD3 AND
                        FIELDNAME <> FIELD4 AND
                        FIELDNAME <> FIELD5.
  DELETE T_CATALOG.
ENDLOOP.

DESCRIBE TABLE T_CATALOG LINES SY-TABIX.
IF SY-TABIX > 1.

  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
  EXPORTING
    IT_FIELDCATALOG = T_CATALOG
  IMPORTING
    EP_TABLE = MY_TABLE
  EXCEPTIONS
    GENERATE_SUBPOOL_DIR_FULL = 1
    OTHERS = 2.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  ASSIGN MY_TABLE->* TO <DYN_TABLE>.
* Create a workarea
  CREATE DATA MY_LINE LIKE LINE OF <DYN_TABLE>.
  ASSIGN MY_LINE->* TO <WA>.
ENDIF.

Max

Read only

Former Member
0 Likes
1,062

Hi ,

TABLES: mara.

SELECT-OPTIONS: s_matnr FOR mara-matnr,

s_mtart FOR mara-mtart.

DATA: BEGIN OF itab OCCURS 0,

matnr LIKE mara-matnr,

mtart LIKE mara-mtart,

END OF itab,

wa LIKE itab.

AT SELECTION-SCREEN OUTPUT.

clear itab.

wa-matnr = s_matnr-low.

wa-mtart = s_mtart-low.

APPEND wa TO itab.

I ve used only 2 select-options.

u use according to ur requirement.

Hope this helps .

Read only

Former Member
0 Likes
1,062

hi

have a look

[Link1|http://www.sap-img.com/ab030.htm]

[link2|https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/dynamic%252binternal%252btable]

Regards

Shashi

Read only

Former Member
0 Likes
1,062

Hey max,

I dont have five fields on screen.

This is my selection screen.

parameters : p_tname(10) type c.

parameters : p_fname type rlgrap-filname.

In the second parameter, the user will give the field names as KUNNR,LAND1,NAME1,ORT01,TELF1

Then i am splitting this p_fname into five fields separated by ','

Now i am getting the five values in five fields f1 to f5.

Now how to create internal table with these 5 fields.

I am not able to proceed.

What is the use of the function module LVC_FIELDCATALOG_MERGE for creating Internal Table

Help will be appreciated.

Thanks,

Ibrahim

Read only

Former Member
0 Likes
1,062

Hi,

Try this,

DATA : wa_fcat TYPE lvc_s_fcat,

it_fcat TYPE lvc_t_fcat.

DATa: c_f1 type c value 'F1'.

FIELD-SYMBOLS: <fs_table> TYPE table,

<fs_any> TYPE ANY,

<fs_wa> TYPE ANY,

DATA : it_tab TYPE REF TO data.

*creating the structure

wa_fcat-fieldname = 'F1'.

wa_fcat-tabname = 'IT_TAB'.

wa_fcat-reptext = 'F1'.

wa_fcat-outputlen = 35.

APPEND wa_fcat TO it_fcat.

wa_fcat-fieldname = 'F2'.

wa_fcat-tabname = 'IT_TAB'.

wa_fcat-reptext = 'F2'.

wa_fcat-outputlen = 35.

APPEND wa_fcat TO it_fcat.

wa_fcat-fieldname = 'F3'.

wa_fcat-tabname = 'IT_TAB'.

wa_fcat-reptext = 'F3'.

wa_fcat-outputlen = 35.

APPEND wa_fcat TO it_fcat.

like this upto F5.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

  • i_style_table =

it_fieldcatalog = it_fcat

  • i_length_in_byte =

IMPORTING

ep_table = it_tab

  • e_style_fname =

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ASSIGN it_tab->* TO <fs_table> .

CREATE DATA wa_line LIKE LINE OF <fs_table>.

ASSIGN wa_line->* TO <fs_wa>.

loop at it into wa. "table in which ur getting values

ASSIGN COMPONENT c_f1 OF STRUCTURE <fs_wa> TO <fs_any>.

<fs_any> = wa-f1.

UNASSIGN <fs_any>.

like thsi assign f2 to f5....

  • Append the Values Into The Table(Field Symbol).

APPEND <fs_wa> TO <fs_table>.

CLEAR: <fs_wa>

ENDLOOP.

Hope u will get the solution.

Thanks & Regards,

Anagha Deshmukh