‎2009 Aug 23 10:10 AM
HI all ,
There is problem which i need to Move data from table one to table 2 with differnt structure the
for instance
table 1
pernr comments
123 AAA
123 BBB
123 CCC
456 ZZZZ
456 TTTT
456 GGGG
456 SSSS
What I want :
table 2
pernr line 1 line 2 line3
11 AAA BBBB CCCC
22 ZZZZ TTTT GGGGThe code :
TYPES:BEGIN OF ty_tab,
pernr TYPE numc10,
comments TYPE char10,
END OF ty_tab.
DATA:it_emp_com TYPE TABLE OF ty_tab,
ls_emp_com LIKE LINE OF it_emp_com.
TYPES:BEGIN OF ty_tab2,
pernr TYPE numc10,
line1 TYPE char10,
line2 TYPE char10,
line3 TYPE char10,
line4 TYPE char10,
END OF ty_tab2.
DATA: it_emp_com2 TYPE TABLE OF ty_tab2,
ls_emp_com2 LIKE LINE OF it_emp_com.
ls_emp_com-pernr = 123.
ls_emp_com-comments = 'AAA'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 123.
ls_emp_com-comments = 'BBB'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 123.
ls_emp_com-comments = 'CCC'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 456.
ls_emp_com-comments = 'ZZZ'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 456.
ls_emp_com-comments = 'GGGG'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 456.
ls_emp_com-comments = 'CCCC'.
APPEND ls_emp_com TO it_emp_com.
ls_emp_com-pernr = 456.
ls_emp_com-comments = 'SSSS'.
APPEND ls_emp_com TO it_emp_com.
FIELD-SYMBOLS : <fs> TYPE any.
REFRESH : it_emp_com2.
DATA: v_flg TYPE n,
v_field TYPE char20.
LOOP AT it_emp_com INTO ls_emp_com.
AT NEW pernr.
MOVE 1 TO v_flg.
ENDAT.
MOVE ls_emp_com-pernr TO ls_emp_com2-pernr.
CONCATENATE 'LS_EMP_COM2-LINE' v_flg INTO v_field.
CONDENSE v_field NO-GAPS.
ASSIGN v_field TO <fs>.
WRITE ls_emp_com-comments TO <fs>.
v_flg = v_flg + 1.
AT END OF pernr.
CLEAR v_flg.
APPEND ls_emp_com TO it_emp_com2.
ENDAT.
ENDLOOP.I try like this and i wont works
any idea what i miss here ?
Regards
Joy
‎2009 Aug 23 10:29 AM
Hi, Joy
Number of lines in itab2 are fix from Line1 to Line4 ? if yes than Please Test the Following Sample Code
TYPES: BEGIN OF ty_tab1,
pernr TYPE numc10,
comments TYPE char10,
END OF ty_tab1.
TYPES: BEGIN OF ty_tab2,
pernr TYPE numc10,
line1 TYPE char10,
line2 TYPE char10,
line3 TYPE char10,
line4 TYPE char10,
END OF ty_tab2.
DEFINE assign_val .
assign component &1 of structure <dyn_wa> to <dyn_val>.
<dyn_val> = &2.
END-OF-DEFINITION.
DATA: it_ty1 TYPE STANDARD TABLE OF ty_tab1,
wa_ty1 LIKE LINE OF it_ty1,
it_ty2 TYPE STANDARD TABLE OF ty_tab2,
wa_ty2 LIKE LINE OF it_ty2,
count_pernr TYPE i.
wa_ty1-pernr = 1.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-comments = 'CCC'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'DDD'. APPEND wa_ty1 TO it_ty1.
wa_ty1-pernr = 2.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-comments = 'CCC'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'DDD'. APPEND wa_ty1 TO it_ty1.
SORT: it_ty1 BY pernr comments.
LOOP AT it_ty1 INTO wa_ty1.
AT NEW pernr.
count_pernr = 1.
ENDAT.
wa_ty2-pernr = wa_ty1-pernr.
CASE count_pernr.
WHEN 1.
wa_ty2-line1 = wa_ty1-comments.
WHEN 2.
wa_ty2-line2 = wa_ty1-comments.
WHEN 3.
wa_ty2-line3 = wa_ty1-comments.
WHEN 4.
wa_ty2-line4 = wa_ty1-comments.
WHEN OTHERS.
ENDCASE.
ADD: 1 TO count_pernr.
AT END OF pernr.
APPEND wa_ty2 TO it_ty2.
ENDAT.
ENDLOOP.Please Reply in case of else Requirement.
Best Regards,
Faisal
‎2009 Aug 23 10:29 AM
Hi, Joy
Number of lines in itab2 are fix from Line1 to Line4 ? if yes than Please Test the Following Sample Code
TYPES: BEGIN OF ty_tab1,
pernr TYPE numc10,
comments TYPE char10,
END OF ty_tab1.
TYPES: BEGIN OF ty_tab2,
pernr TYPE numc10,
line1 TYPE char10,
line2 TYPE char10,
line3 TYPE char10,
line4 TYPE char10,
END OF ty_tab2.
DEFINE assign_val .
assign component &1 of structure <dyn_wa> to <dyn_val>.
<dyn_val> = &2.
END-OF-DEFINITION.
DATA: it_ty1 TYPE STANDARD TABLE OF ty_tab1,
wa_ty1 LIKE LINE OF it_ty1,
it_ty2 TYPE STANDARD TABLE OF ty_tab2,
wa_ty2 LIKE LINE OF it_ty2,
count_pernr TYPE i.
wa_ty1-pernr = 1.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-comments = 'CCC'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'DDD'. APPEND wa_ty1 TO it_ty1.
wa_ty1-pernr = 2.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-comments = 'CCC'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'DDD'. APPEND wa_ty1 TO it_ty1.
SORT: it_ty1 BY pernr comments.
LOOP AT it_ty1 INTO wa_ty1.
AT NEW pernr.
count_pernr = 1.
ENDAT.
wa_ty2-pernr = wa_ty1-pernr.
CASE count_pernr.
WHEN 1.
wa_ty2-line1 = wa_ty1-comments.
WHEN 2.
wa_ty2-line2 = wa_ty1-comments.
WHEN 3.
wa_ty2-line3 = wa_ty1-comments.
WHEN 4.
wa_ty2-line4 = wa_ty1-comments.
WHEN OTHERS.
ENDCASE.
ADD: 1 TO count_pernr.
AT END OF pernr.
APPEND wa_ty2 TO it_ty2.
ENDAT.
ENDLOOP.Please Reply in case of else Requirement.
Best Regards,
Faisal
‎2009 Aug 23 11:44 AM
HI Faisal,
Thanks a lot!
There is a way to do that dynamically (it_ty2),i.e. with table that is not have restriction
for example user can have 20 comments .
Thanks
Joy
‎2009 Aug 23 4:10 PM
Hi, Joy
Test the Bellow Sample Code it should work for you.
First Part of CodeREPORT zfsl_sdn_tests.
TYPES: BEGIN OF ty_tab1,
pernr TYPE numc10,
comments TYPE char10,
END OF ty_tab1.
DEFINE assign_val.
assign component &1 of structure <dyn_wa> to <dyn_val>. <dyn_val> = &2.
END-OF-DEFINITION.
DATA: it_ty1 TYPE STANDARD TABLE OF ty_tab1,
wa_ty1 LIKE LINE OF it_ty1,
it_all_pernr TYPE STANDARD TABLE OF ty_tab1,
wa_all_pernr like LINE OF it_all_pernr,
count_pernr TYPE i,
count_column TYPE i,
max_pernr TYPE i,
column_no TYPE n,
column_name(20),
count(20),
it type ref to data,
wa type ref to data,
it_fieldcatalog TYPE STANDARD TABLE OF lvc_s_fcat WITH HEADER LINE,
wa_fieldcatalog TYPE lvc_s_fcat.
field-symbols : <dyn_it> type standard table,
<dyn_val> TYPE any,
<dyn_wa> type data,
<dyn_pernr> type any,
<dyn_comments> type any.
wa_ty1-pernr = 1.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-pernr = 2.
wa_ty1-comments = 'AAA'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'BBB'. APPEND wa_ty1 TO it_ty1.
wa_ty1-comments = 'CCC'. APPEND wa_ty1 TO it_ty1. wa_ty1-comments = 'DDD'. APPEND wa_ty1 TO it_ty1.
SORT: it_ty1 BY pernr comments.
‎2009 Aug 23 4:13 PM
Next and Final Part of CodeLOOP AT it_ty1 INTO wa_ty1.
AT NEW pernr.
wa_all_pernr-pernr = wa_ty1-pernr.
APPEND wa_all_pernr to it_all_pernr.
count_pernr = 0.
ENDAT.
add: 1 to count_pernr.
at END OF pernr.
IF max_pernr < count_pernr.
max_pernr = count_pernr.
ENDIF.
ENDAT.
ENDLOOP.
CLEAR:it_fieldcatalog.
it_fieldcatalog-fieldname = 'PERNR'.
it_fieldcatalog-col_pos = 1.
it_fieldcatalog-outputlen = '10'.
APPEND it_fieldcatalog TO it_fieldcatalog.
column_no = 2.
count = 1.
DO max_pernr TIMES.
clear: it_fieldcatalog.
CONDENSE: count.
CONCATENATE: 'line' count INTO column_name.
it_fieldcatalog-fieldname = column_name.
it_fieldcatalog-col_pos = column_no.
it_fieldcatalog-outputlen = '15'.
APPEND it_fieldcatalog TO it_fieldcatalog.
add: 1 to column_no,
1 to count.
ENDDO.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcatalog[]
IMPORTING
ep_table = it
EXCEPTIONS
generate_subpool_dir_full = 1
others = 2.
assign it->* to <dyn_it>.
create data wa like line of <dyn_it> .
assign wa->* to <dyn_wa> .
LOOP AT it_all_pernr into wa_all_pernr.
count_column = 2.
count = 1.
assign_val 'PERNR' wa_all_pernr-pernr.
LOOP AT it_ty1 into wa_ty1 WHERE pernr = wa_all_pernr-pernr.
CONDENSE: count.
CONCATENATE: 'LINE' count INTO column_name.
assign_val column_name wa_ty1-comments.
add: 1 to count.
ENDLOOP.
APPEND <dyn_wa> to <dyn_it>.
clear: <dyn_wa>.
ENDLOOP.Reply me back in case of any Confusion about the Code.
Best Regards,
Faisal
‎2009 Aug 23 4:23 PM
HI Faisal ,
Thanks its works !
one question is if there is a way to do that without using the method create_dynamic_table?
maybe with create data statement
Thanks again
Joy
‎2009 Aug 23 5:11 PM
Hi, Joy
I think you are taking about to create the dynamic table in the following way.
DATA: it TYPE REF TO data.
FIELD-SYMBOLS: <dyn_it> TYPE TABLE.
data: tabname TYPE string VALUE 'T247'.
CREATE DATA it TYPE TABLE OF (tabname).
ASSIGN it->* TO <dyn_it>.
SELECT * FROM (tabname) INTO CORRESPONDING FIELDS OF TABLE <dyn_it>
WHERE spras = 'EN'.But i think using the above way we can only create the dynamic tables Like Database tables. because we have the requirement of variable number of columns here, So we will have to use that way.
Please Correct me if wrong or let me know if you have some idea about it, waiting for your response.
Thanks and Best Regards,
Faisal
‎2009 Aug 23 5:37 PM
Another option will be using RTTS class
https://wiki.sdn.sap.com/wiki/display/Snippets/WrappercodeforcreatingDynamicInternaltableusingRTTS
a®