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

Create a dynamic internal table horizontally from another internal table

Former Member
0 Likes
1,418

Dear SAP HR users,

Kindly assist me on this matter. Let say I have an internal table like below;

ATEXT STDAZ

Annual Leave 8.00

Sick Leave 4.00

Is it possible to create another table based on the table above for example;

Annual Leave Sick Leave

8 4

Thanks in advance !

1 ACCEPTED SOLUTION
Read only

rthoodi
Active Participant
0 Likes
1,009

Understabd the following code to populate the dynamic internal table

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = y_i_fcat

importing

ep_table = y_i_dyn.

assign y_i_dyn->* to <y_i_dyn>.

create data y_wa_dyn like line of <y_i_dyn>.

assign y_wa_dyn->* to <y_wa_dyn>.

*Move data from Final table to dynamic table <y_i_dyn>

loop at y_i_final into y_wa_final.

assign component 'VBELN' of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-vbeln.

y_lv_cntr = y_lv_cntr + 1.

concatenate 'PARVW' '-' y_lv_cntr into y_lv_parvw.

assign component y_lv_parvw of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-parvw.

concatenate 'KUNNR' '-' y_lv_cntr into y_lv_kunnr.

assign component y_lv_kunnr of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-kunnr.

concatenate 'ADRNR' '-' y_lv_cntr into y_lv_adrnr.

assign component y_lv_adrnr of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-adrnr.

at end of vbeln.

append <y_wa_dyn> to <y_i_dyn>.

free: <y_wa_dyn>,<y_v_fld>.

clear y_lv_cntr.

endat.

endloop.

Regards

RK

6 REPLIES 6
Read only

MarcinPciak
Active Contributor
0 Likes
1,009

Yes, this is feasible. Refer the below snippet.


DATA: BEGIN OF itab OCCURS 0,
        atext TYPE atext,
        stdaz TYPE stdaz,
      END OF itab.

DATA: it_fcat TYPE lvc_t_fcat WITH HEADER LINE.
DATA: l_fieldname TYPE string,
      l_idx(3) TYPE n,
      l_lines type i.
DATA: r_new_tab TYPE REF TO data.

FIELD-SYMBOLS: <new_tab> TYPE INDEX TABLE,
               <wa_tab> TYPE ANY,
               <comp> TYPE ANY.

itab-atext = 'Annual Leave'.
itab-stdaz = 8.
APPEND itab.
itab-atext = 'Sick Leave'.
itab-stdaz = 4.
APPEND itab.


LOOP AT itab.
  "new field name
  it_fcat-fieldname = itab-atext.
  replace ` ` in it_fcat-fieldname with ''. "AnnualLeave, SickLeave
  "make this fiels of same type as PA2006-STDAZ
  it_fcat-ref_field = 'STDAZ'.
  it_fcat-ref_table = 'PA2001'.
  it_fcat-scrtext_s = it_fcat-scrtext_m = it_fcat-scrtext_l = itab-atext. "Annnual Leave, Sick Leave
  APPEND it_fcat.
ENDLOOP.

"create this new table
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog           = it_fcat[]
  IMPORTING
    ep_table                  = r_new_tab
  EXCEPTIONS
    generate_subpool_dir_full = 1
    OTHERS                    = 2.

"address the content of that new empty table
ASSIGN r_new_tab->* TO <new_tab>.

"now you need to get for each 2 lines in ITAB one line in <NEW_TAB>
DESCRIBE TABLE itab lines l_lines.
l_lines = l_lines / 2.
l_idx = 1.

DO l_lines TIMES. "number of records in itab / 2
  APPEND INITIAL LINE TO <new_tab> ASSIGNING <wa_tab>.

  DO 2 TIMES.
    READ TABLE itab INDEX l_idx.
    IF sy-subrc = 0.
      ASSIGN COMPONENT sy-index OF STRUCTURE <wa_tab> TO <comp>.
      IF sy-subrc = 0.
        <comp> = itab-stdaz.
      ENDIF.
      l_idx = l_idx + 1.
    ENDIF.
  ENDDO.
ENDDO.

As you may see you can't really have columns in <NEW_TAB> named "Annual Leave" and "Sick Leave" as there musn't be a space in names. Anyhow if you replace it with "AnnualLeave" or "SickLeave" this is acceptable. Please note, however, that these names must be unique, so that the new column names are also unique.

The rest is just a matter of generating new dynamic table and moving data from ITAB table to <new_tab>.

Regards

Marcin

Read only

Former Member
0 Likes
1,009

Hi,

You can also create a Create a dynamic structure or RTTS type object and then Create a dynamic internal table and workarea using the RTTS type object.

Refer to my article for a code sample and explanation-

http://divulgesap.com/blog.php?p=MTI4

Hope it helps.

Regards,

Ravikiran

Read only

Former Member
0 Likes
1,009

Thanks for the help so far, I've managed to create the dynamic table and now I need to display it in ALV, anyone can help me on this ?

The scenario:

First table containing all the necessary information such as the pernr, age, etc etc

Declared with a table T_DATA

Second table containing leave information where it stores the pernr and the values according to the absence type.

Declared dynamic table <FS_ITAB>, the sample;

Pernr LT_0100 LT_0101 LT_0132

141 0 0 4

How should I display this two table in ALV ? So far I've managed to display the first table without any problem but im stucked on the second table.

Should I combined both tables first then display in ALV ? Or do a separate command for ALV for both tables.

Thanks in advance !

Read only

0 Likes
1,009

Yes, you have to combine both tables' structures into one new table, move data from both to corresponding fields in this new table and then pass that one to ALV.

Regards

Marcin

Read only

rthoodi
Active Participant
0 Likes
1,010

Understabd the following code to populate the dynamic internal table

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = y_i_fcat

importing

ep_table = y_i_dyn.

assign y_i_dyn->* to <y_i_dyn>.

create data y_wa_dyn like line of <y_i_dyn>.

assign y_wa_dyn->* to <y_wa_dyn>.

*Move data from Final table to dynamic table <y_i_dyn>

loop at y_i_final into y_wa_final.

assign component 'VBELN' of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-vbeln.

y_lv_cntr = y_lv_cntr + 1.

concatenate 'PARVW' '-' y_lv_cntr into y_lv_parvw.

assign component y_lv_parvw of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-parvw.

concatenate 'KUNNR' '-' y_lv_cntr into y_lv_kunnr.

assign component y_lv_kunnr of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-kunnr.

concatenate 'ADRNR' '-' y_lv_cntr into y_lv_adrnr.

assign component y_lv_adrnr of structure <y_wa_dyn> to <y_v_fld>.

<y_v_fld> = y_wa_final-adrnr.

at end of vbeln.

append <y_wa_dyn> to <y_i_dyn>.

free: <y_wa_dyn>,<y_v_fld>.

clear y_lv_cntr.

endat.

endloop.

Regards

RK

Read only

Former Member
0 Likes
1,009

Thanks for the help guys.