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
1,684

Hi,

I have date range in selection options. I have to display the dates in the columns of ALV what will dynamically generate. In my internal table i Have 2 column and then date that will be dynamic. After that i will calculate and show the values under that dates.

DES VAL 02.05.2008 03.05.2008 04.05.2008

S10 COD 40.6700 40.5700 40.7000

S28 15m 40.7130 40.6315 40.7367

S35 30s 40.7570 40.6863 40.7767

S49 45b 40.7870 40.7288 40.8067

S52 60y 40.8166 40.7690 40.8405

S55 75r 40.8408 40.8029 40.8719

S8 90e 40.8650 40.8386 40.9002

Please dont send link or example. Pl be specific to my requirement, how to define that type of internal table,assigning value to it and display.

Thanks

14 REPLIES 14
Read only

Former Member
0 Likes
1,656

Hi,

Check the code below:

REPORT ZYKTEST3 .

DATA: d_ref TYPE REF TO data,
          d_ref2 TYPE REF TO data,
          i_alv_cat TYPE TABLE OF lvc_s_fcat, 
          ls_alv_cat LIKE LINE OF i_alv_cat.

TYPES: tabname LIKE dcobjdef-name ,
             fieldname LIKE dcobjdef-name,
             desc LIKE dntab-fieldtext.

Select-options: s_date for sy-datum. ----> Input date range

DATA: BEGIN OF itab OCCURS 0.
          * Define your internal table fields
DATA: END OF itab.
ranges: r_date type table of s_date,
            wa_date like line of r_date.

FIELD-SYMBOLS : <f_fs> TYPE table,
                            <f_fs1> TYPE table,
                             <f_fs2> TYPE ANY,
                             <f_fs3> TYPE ANY,
                              <f_fs4> type any,
                              <f_field> TYPE ANY.

loop at s_date.
wa_date-sign = 'I'.
wa_date-option = 'EQ'.
wa_date-low = s_date.
wa_date-high = s_date.
append wa_date to r_date.
endloop.

REFRESH itab.


LOOP AT itab.
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
ENDLOOP.
loop at r_date into wa_date.
ls_alv_cat-fieldname = wa_date-fieldname.
ls_alv_cat-ref_field = wa_Date-fieldname.
ls_alv_cat-seltext = wa_date-fieldtext.
ls_alv_cat-reptext = wa_date-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
endloop.

*internal table build 
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.

ASSIGN d_ref->* TO <f_fs>. ----> Dynamic table creation with fields of the table

Regards

Kannaiah

Read only

0 Likes
1,656

Hi

Kannaiah Kavuri

Thanks for your reply..

can u explain it more. I have 2 field fixed in my internal table and rest dates are dynamic.

how to declear it?

DATA: BEGIN OF itab OCCURS 0.
              DES(4) type c,
              VAL(3) type c,

???????????????????????????
          DATA: END OF itab.

And how to declear FIELD-SYMBOL according to my internal table?

FIELD-SYMBOLS : <f_fs> TYPE table,
                            <f_fs1> TYPE table,
                             <f_fs2> TYPE ANY,
                             <f_fs3> TYPE ANY,
                              <f_fs4> type any,
                              <f_field> TYPE ANY.

Read only

0 Likes
1,656

Hi,

1. Declare the internal table with two fields only.

2. Take the select options data into a range table.

3. Create a field catalog by taking the two fields from the internal table.

4. Also append the data in ranges to the field catalog.

5. call the method with exporting parameter field catalog and import the internal table d_ref.

6. Assign it to <f_fs>.

Now you can use the table <f_fs>.

DATA: d_ref TYPE REF TO data,
           i_alv_cat TYPE TABLE OF lvc_s_fcat, " To build the field catalog 
           ls_alv_cat LIKE LINE OF i_alv_cat.
 
Select-options: s_date for sy-datum. ---->Your Input date range
 
* Declaration of your itab
DATA: BEGIN OF itab OCCURS 0.
              DES(4) type c,
              VAL(3) type c,
DATA: END OF itab.

* Take the range for your select option
Data:  r_date type range of s_date,
            wa_date like line of r_date.
 
FIELD-SYMBOLS : <f_fs> TYPE table. "This is your final dynamic table

* Take the select option data into ranges                    
loop at s_date.
wa_date-sign = 'I'.
wa_date-option = 'EQ'.
wa_date-low = s_date.
wa_date-high = s_date.
append wa_date to r_date.
endloop.

* Build a field catalog with two fields of your internal table. 
LOOP AT itab.
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
ENDLOOP.
* And also with the data from ranges table also
loop at r_date into wa_date.
ls_alv_cat-fieldname = wa_date-low.
ls_alv_cat-ref_field = wa_Date-low.
ls_alv_cat-seltext = wa_date-low.
ls_alv_cat-reptext = wa_date-low.
APPEND ls_alv_cat TO i_alv_cat.
endloop.
 
*Calling the method to create dynamic internal table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
 
ASSIGN d_ref->* TO <f_fs>. ----> Assigning the final table,

Read only

0 Likes
1,656

HI

how to declear

 2. Take the select options data into a range table.

what u have wrote is giving syntax error.

DATA:  R_DATE TYPE RANGE OF S_DATE,
              WA_DATE LIKE LINE OF R_DATE.

And be specific to this..

ASSIGN d_ref->* TO <f_fs>. ----> Assigning the final table,

Read only

0 Likes
1,656

Hi,

Check the code below:

DATA: d_ref TYPE REF TO data,
      d_ref2 TYPE REF TO data,
      i_alv_cat TYPE TABLE OF lvc_s_fcat,
      ls_alv_cat LIKE LINE OF i_alv_cat,
      v_var1 TYPE i.

SELECT-OPTIONS: s_date FOR sy-datum.

DATA: BEGIN OF itab OCCURS 0,
      des(4) TYPE c,
      val(3) TYPE c,
      END OF itab.
DATA: r_date TYPE RANGE OF s_date,
      wa_date LIKE LINE OF r_date.

FIELD-SYMBOLS : <f_fs> TYPE table.

LOOP AT s_date.
wa_date-sign = 'I'.
wa_date-option = 'BT'.
wa_date-low = s_date-low.
wa_date-high = s_date-high.
APPEND wa_date TO r_date.
ENDLOOP.

v_var1 = s_date-high - s_date-low.

ls_alv_cat-fieldname = 'DES'.
ls_alv_cat-ref_field = itab-des.
ls_alv_cat-seltext = itab-des.
ls_alv_cat-reptext = itab-des.
APPEND ls_alv_cat TO i_alv_cat.
ls_alv_cat-fieldname = 'VAL'.
ls_alv_cat-ref_field = itab-val.
ls_alv_cat-seltext = itab-val.
ls_alv_cat-reptext = itab-val.
APPEND ls_alv_cat TO i_alv_cat.

DO v_var1 TIMES.
  IF wa_date-low LE wa_date-high.
ls_alv_cat-fieldname = wa_date-low.
ls_alv_cat-ref_field = wa_date-low.
ls_alv_cat-seltext = wa_date-low.
ls_alv_cat-reptext = wa_date-low.
APPEND ls_alv_cat TO i_alv_cat.
wa_date-low = wa_date-low + 1.
ENDIF.
ENDDO.

*internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.

ASSIGN d_ref->* TO <f_fs>.

Regards

Kannaiah

Read only

0 Likes
1,656

Thanks a lot.

So <f_fs>. is my internal table. So now i have to fill the internal table. So now i have to put data in that internal table. So where anh how to use SELECT statement for that <f_fs> internal table.

suppose my select statement is like this...

select DES
                  VAL
                  TCURR  <dynamic field values>
       from TCURR
      into corresponding fields of itab
     where date in s_date.

Read only

0 Likes
1,656

Hi,

FIELD-SYMBOLS :   <f_fs2> type any,
                                <F_FS3> TYPE ANY,
                              <F_FS4> TYPE ANY,
                              <F_FS5> TYPE ANY,
                             <F_WA> TYPE ANY.
After that 
CREATE DATA G_WA LIKE LINE OF <F_FS>.
ASSIGN G_WA->* TO <F_WA>.

* Declare an internal table like
data: v_var2 type i.
types: begin of t_tab occurs 0,
        des(3) type c,
        val(3) type c,
        date type sy-datum,
        value type c,
      end of t_tab.
data: itab type table of t_tab,
        wa type t_tab.
select des val date value
   into corresponding fields of itab
  from TCURR
where date in s_date.

loop at itab assigning <f_fs2>.
ASSIGN COMPONENT 'DES' OF STRUCTURE <F_FS2>   TO <F_FS3>.
ASSIGN COMPONENT 'DES' OF STRUCTURE <F_WA> TO <F_FS4>.
<F_FS4> = <F_FS3>.
clear: <f_fs3>, <f_fs4>.
ASSIGN COMPONENT 'VAL' OF STRUCTURE <F_FS2>   TO <F_FS3>.
ASSIGN COMPONENT 'VAL' OF STRUCTURE <F_WA> TO <F_FS4>.
<F_FS4> = <F_FS3>.

ASSIGN COMPONENT 'VALUE' OF STRUCTURE <F_FS2> TO <F_FS3>.
ASSIGN COMPONENT v_var1 OF STRUCTURE <F_WA> TO <F_FS4>.
<F_FS4> = <F_FS3>.
v_var2 = v_var2 + 1.
APPEND <F_WA> TO <F_FS>.
CLEAR <F_WA>.
endloop.

Regards

Kannaiah

Read only

0 Likes
1,656

Hi

Thanks a lot,

But now the problem is if im giving a range of date

02052008 to 05052008

its giving error

The type "" has no structure and therefore no comp 6 -20080502

So what to do now?

Read only

0 Likes
1,656

Hi,

You need to get the data into dynamic internal table and populate output by using write. then you wont get the error.

Regards

Kannaiah

Read only

0 Likes
1,656

Hallo,

i'm beginner at ABAP and i try to create a dynamic table. I found your answer here and it helped me to get as far as calling method cl_alv_table_create=>create_dynamic_table but i get an error. Can you help me here? What You mean by populating the internal table?!

Thanks a lot in advance& best greetings!

George

Read only

0 Likes
1,656

Hi again!

I just found the answer. I didn't supply values for ref_field and ref_table.

Now it works!

Thanks a lot!

Read only

peter_ruiz2
Active Contributor
0 Likes
1,656

hi,

here is an example on how to create a dynamic table


data:
  git_fldct type lvc_t_fcat,
  git_otput type ref to data.

field-symbols:
<git-otput> type table,
<lwa_otput> type any,
<fs_field> type any.

START-OF-SELECTION
PERFORM fl_buildcatx.

assign git_otput->* to <git_otput>

loop at <git_otput> assigning <lwa_otput>
  assign component 'STATS'
   of structure <lwa_otput> to <fs_field>.

  write / <fs_field>.
endloop.

form fl_buildfcatx.
  PERFORM fl_defineitab
   TABLES git_fldct
   USING: 'STATS'       space 0 1  'C' 'NUMC', "expand/collapse
          'ACCOUNTS'    space 0 50 'L' 'CHAR', "accounts
          'TOTAL_ACTL1' space 2 15 'R' 'CURR', "total - current actual
          'TOTAL_ACTL2' space 2 15 'R' 'CURR', "total - prior actual
          'TOTAL_VARC1' space 2 15 'R' 'CURR', "total - variance 1
          'TOTAL_AGE1'  space 0 8  'R' 'CHAR', "total - age 1
          'TOTAL_BUDGT' space 2 15 'R' 'CURR', "total - budget
          'TOTAL_VARC2' space 2 15 'R' 'CURR', "total - variance 2
          'TOTAL_AGE2'  space 0 8  'R' 'CHAR'. "total - age 2

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = git_fldct
    IMPORTING
      ep_table                  = git_otput
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.
endform.

FORM fl_defineitab TABLES pt_table STRUCTURE lvc_s_fcat
                   USING  pi_fname
                          pi_ctext
                          pi_dcmal
                          pi_outln
                          pi_justx
                          pi_dtype.
*** local work area
  DATA:
    lwa_fldct TYPE lvc_s_fcat.

  lwa_fldct-fieldname = pi_fname.
  lwa_fldct-coltext   = pi_ctext.
  lwa_fldct-decimals  = pi_dcmal.
  lwa_fldct-outputlen = pi_outln.
  lwa_fldct-just      = pi_justx.
  lwa_fldct-datatype  = pi_dtype.

  APPEND lwa_fldct TO pt_table.
  CLEAR lwa_fldct.
ENDFORM.                    " fl_defineitab

regards,

Peter

Read only

Former Member
0 Likes
1,656

thanks a lot

Read only

0 Likes
1,656

Reward points if my solution helped you.

Regards

Kannaiah