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 -- urgent

Former Member
0 Likes
1,590

HI All,

I want to create a dynamic internal table, my exact requirement is,

on selection screen i have date field as select option, if some one enteres 1-05-2006 to 31-10-2006 then i have to display months from may to oct , i have a internal table where i have data for all months,also tell how to transfer data from this internal table to dynamic internal table.

if some can post the code then that will be extremly helpfule.

15 REPLIES 15
Read only

Former Member
0 Likes
1,374

hi you can create dynamic internal tables using

CL_ALV_TABLE_CREATE

And for the above issue.

you can create a simple internal table and select the data using entires made by user.

you have specify start date and end date (range) in you select query.

oncwe the data is selected in simple internal table just sort it and display

hope this helps.

Award helpful answers

Read only

0 Likes
1,374

Hi Sundeep,

Can u send a code , with example of dynamic internal table usage.......

With Regards

Bhaskar Rao.M

Read only

Former Member
0 Likes
1,374

Hi Shilpa,

you can use field-symbol for this.

e.g.

DATA: dref TYPE REF to data.

FIELD-SYMBOLS: <fs_tname>, "type any.

<FS>.

ASSIGN itab TO <FS>.

CREATE DATA dref TYPE (<FS>).

assign dref->* to <fs_tname> .

regards

Deepak .

Read only

0 Likes
1,374

Code to create the dynamic internal table is this -



parameter p_table type tabname.

field-symbols <tab> type table.
field-symbols <tab1> type any.


types: begin of itab,
      t_name type tabname,
      t_ref type ref to data,
      end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.
perform print_table using p_table.
*&---------------------------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM fetch_data  USING P_TABLE1 type tabname.

data itab2 type itab.
itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM.                    " fetch_data
*&---------------------------------------------------------------------*
*&      Form  print_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM print_table  USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.
DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.
*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.
READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.
*WRITE:/ p_table1.
NEW-LINE.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: <field>,'    '.
ENDDO.
ENDDO.
ENDFORM.                    " print_table

also u can use the below logic to find the month name from the dates in selection screen as follows -



data : d1(8) value '20051215',
c1(2),
c2(10).
c1 = d1+3(2).

select ktx into x_ktx from T247 where spras = 'E'.

concatenate d1+6(2) x_ktx d1+0(4) into c2.
write c2.
or use FM - CONVERSION_EXIT_SDATE_OUTPUT

Read only

Former Member
Read only

Former Member
0 Likes
1,374

Shilpa,

See the below thread..... for a sample code.

Thanks

Read only

Former Member
0 Likes
1,374

----


DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

----


  • Create dynamic intenal table to store the values

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = gt_fieldcat

IMPORTING

ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

----


FORM fill_int_table .

DATA: l_col TYPE sy-index.

DATA: ls_text(40) TYPE c.

FIELD-SYMBOLS: <ls_table>.

FIELD-SYMBOLS: <l_field>.

ASSIGN LOCAL COPY OF INITIAL LINE OF <gt_table> TO <ls_table>.

LOOP AT itabdown.

LOOP AT itabdown-rslt INTO ls_text.

l_col = l_col + 1.

ASSIGN COMPONENT l_col OF STRUCTURE <ls_table> TO <l_field>.

<l_field> = ls_text.

ENDLOOP.

APPEND <ls_table> TO <gt_table>.

l_col = 0.

ENDLOOP.

ENDFORM. " fill_int_table

Read only

raviprakash
Product and Topic Expert
Product and Topic Expert
0 Likes
1,374

Hi Shilpa,

Here is the code to dynamically create an Internal Table :-

FUNCTION zcreate_internal_table.

*"----


""Local interface:

*" IMPORTING

*" VALUE(I_TABNAME) TYPE DD02L-TABNAME

*" EXPORTING

*" REFERENCE(EP_TABLE) TYPE REF TO DATA

*"----


TYPE-POOLS : slis.

DATA : lv_fcat TYPE slis_t_fieldcat_alv,

lt_fieldcat TYPE lvc_t_fcat,

ls_fieldcat TYPE slis_fieldcat_alv,

ls_fcat TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fs_table> TYPE table.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = i_tabname

CHANGING

ct_fieldcat = lv_fcat.

LOOP AT lv_fcat INTO ls_fieldcat.

MOVE-CORRESPONDING ls_fieldcat TO ls_fcat.

APPEND ls_fcat TO lt_fieldcat.

ENDLOOP.

  • You can add new fields for your dynamic table here. This can be done by

  • appending new entries to the Field Catalogue. After appneding the new entries

  • in the field catalogue, call the following method and create the new internal table.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_fieldcat

IMPORTING ep_table = ep_table.

ENDFUNCTION.

*Use the following statement after the calling of the above function to

*get the structure of the internal table of the structure similar to the

*passed table name :-

ASSIGN ep_table->* to <fs_table>.

NOTE: The above solution creates a new internal table.

Please reward points if you are satisfied with the solution.

Also, i can help you with the code for your case, but i need a clear understanding of your requirements. Can you, please put your requirement in a step by step manner, if possible.

Thanks and regards,

Ravi.

Read only

Former Member
0 Likes
1,374
SORT itab by date.
DESCRIBE LINES OF TABLE itab v_cnt.
READ TABLE itab WITH INDEX 1.
IF sy-subrc = 0.
v_month_start = itab-date+4(2).
ENDIF.
LOOP AT ITAB FROM v_cnt TO v_cnt. 
v_month_end  = itab-date+4(2).
CLEAR itab.
ENDLOOP.

CLEAR v_month.
v_month = v_month_start.
DO.
IF v_month <= v_month_end.
CASE v_month.
WHEN '01'.
  WRITE:/  'January'
WHEN '02'.
 WRITE:/  'February'.
WHEN '03'.
  WRITE:/  'March'.
......
ENDCASE.
v_month = v_month_start + 1.
ENDIF.

In this way u can write the header, then display the data from the itab only with in the date range by restricting in the LOOP.

Also refer this for ALV

<b>Assumption: Structure of the dynamic table should be known before hand.(At least in this program, I am not sure how to do it otherwise. Probably the FIELD-SYMBOL stud programmers can help us out).</b>

report  ytest.
data: lt_fieldcatalog type lvc_t_fcat.
data: ls_fieldcatalog type lvc_s_fcat.
field-symbols: <fs_data> type ref to data.
field-symbols: <fs_1>.
field-symbols: <fs_2> type any table.
field-symbols: <fs_3> type ypoll.


data: lt_data type ref to data.

assign lt_data to <fs_data>.

ls_fieldcatalog-fieldname = 'MANDT'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'POLLID'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'TEAM'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'INITIATOR'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'DESCRIPTION'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'APPROVED'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'INITIATED_DATE'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'END_DATE'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'WINNER'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.


call method cl_alv_table_create=>create_dynamic_table
  exporting
    it_fieldcatalog           = lt_fieldcatalog
  importing
    ep_table                  = <fs_data>
  exceptions
    generate_subpool_dir_full = 1
    others                    = 2
        .
if sy-subrc <> 0.
endif.

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

loop at <fs_2> assigning <fs_3>.
  write: <fs_3>-pollid.
endloop.

Message was edited by:

Judith Jessie Selvi

Read only

SantoshKallem
Active Contributor
Read only

Former Member
0 Likes
1,374

HI Ravi,

Thanxs for reply, but still i am not geting how to use it for my requirement, being new to field symbols.

If possible please help me with my code, my requirement is, I am having an simple internal table in which i have all the data and i am using this internal table for alv display and i have provided an download button for alv, when user clicks on donload button, an excel file gets downloaded which has all the columns of internal table irrespective of the what user have entered on selection screen.

Read only

0 Likes
1,374

so you just want to make a selection from a single table and present that in an alv ?

why have you programmed a download button. standard alv layout already provides a download button to excel.

and I guess there is some confusement since you ask for dynamic tables which is quite different from just making an selection for a period.

if you just look at se16n and give you're tablename you can already do what you need and want I guess ? right ?

Read only

Former Member
0 Likes
1,374

Use the Rtti, i.e. the classes and subclasses of cl_abap_typedescr. From release 6.40 on you have the method 'create', i.e. if you use the method

cl_abap_tabledescr=>create

you can create dynamically an internal table. Search in http://help.sap.com/ for 'RTTI' or 'cl_abap_typedescr' in order to get more detailed information.

Best regards,

Thomas

Read only

0 Likes
1,374

he Shilpa how are you doing, it takes quite long for you to reply to a request posted as urgent ?

Read only

Former Member
0 Likes
1,374

Hi Shilpa,

Here is the algorithm for creating dynamic internal table.

1. Create your field catalog either manually or automatically using the function module, LVC_FIELDCATALOG_MERGE. Add more rows to the field catalog table (T_FIELDCAT) at run time.

2. Use the field catalog to create a table dynamically using the method below.

DATA: T_OUTPUT TYPE REF TO DATA

FIELD-SYMBOLS: <T_OUTPUT> TYPE TABLE

Call Method CL_ALV_TABLE_CREATE->CREATE_DYNAMIC_TABLE

Exporting

IT_FIELDCATALOG = T_FIELDCAT

Importing

EP_TABLE = T_OUTPUT

ASSIGN T_OUTPUT->* TO <T_OUTPUT>.

Now the field symbol <T_OUTPUT> is pointing to an output table of the structure that contains the fields which were determined at runtime. Now fill this table with the data and pass <T_OUTPUT> to the method SET_TABLE_FOR_FIRST_DISPLAY and the ALV grid should show the data properly.

Example:

  • the content of itab will be fields of the new table

loop at itab1 into wa1.

Gs_FIELDCAT-TABNAME = 'itab2'.

GS_FIELDCAT-FIELDNAME = wa1-packid.

GS_FIELDCAT-OUTPUTLEN = 2.

GS_FIELDCAT-KEY = space.

GS_FIELDCAT-SELTEXT_L = wa1-packid.

GS_FIELDCAT-COL_POS = 1.

GS_FIELDCAT-JUST = 'L'.

APPEND GS_FIELDCAT TO GT_FIELDCAT.

endloop.

LOOP AT GT_FIELDCAT INTO GS_FIELDCAT.

MOVE-CORRESPONDING GS_FIELDCAT TO ls_fcat.

APPEND ls_fcat TO lt_fieldcat.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_fieldcat

IMPORTING ep_table = t_output.

once you create the table then you can just transfer the data from one table to another

INSERT LINES OF itab1 [FROM idx1] [TO idx2] INTO itab2 [INDEX idx3].

checkf1 for insert statement

hope this helps.

Regards,

kinshuk