‎2013 Jan 04 4:53 AM
Hi All,
I have a small requirement. I have declared an internal table dynamically. Now I have to move data between two tables with not even a single common field. I just want the data should move from Column1 to Column1 and so on. I do not know the no of columns of the second table as it will vary between 1 and 128. The former table from which I have to move the data has 128 columns.
Thanks & Regards,
Chandni Sharma.
‎2013 Jan 04 5:04 AM
loop the first internal table
validate the column of the second internal table and put the table value.
append second internal table
hope it helps,
Vinoth.
‎2013 Jan 04 5:08 AM
Hi Vinoth, my question is I do not have the names of the columns known. How should I validate the columns and there is no single common column between the two. How should move between the two. It Gives me a Dump saying the Two are incompatible and not convertible.
Thanks,
Chandni Sharma.
‎2013 Jan 04 5:11 AM
Since you have to map the nth column of the first internal table to the nth column of the second internal table, you can use the ASSIGN COMPONENT <comp> OF STRUCTURE statement. In <comp> you can use a counter which would represent the column number.
‎2013 Jan 04 5:39 AM
Assign Component will pick the Nth column of first table but how to assign it to the Nth column of the second table.
Thanks,
Chandni Sharma.
‎2013 Jan 04 5:42 AM
I have concatenated the content of first table into a string separated by comma but how to split the string at ',' into columns of the table.
for eg:
if string is 1,2,3,4,5,6.
Table or work area should contain wa-col1 = 1. wa-col2 = 2. wa-col3 = 3. wa-col4 = 4. and so on.
Thanks,
Chandni Sharma.
‎2013 Jan 04 5:45 AM
You can use two field-symbols - one for the source table and one for the target.
‎2013 Jan 04 5:53 AM
Hi ,
try this..
Data : p1 type c,
p2 type c,
p3 type c,
comma type c value','. " comma splitter
now use split..
SPLIT string AT comma into p1 p2 p3....
now move p1... pn to ur work area and append to intab..
one more thing .. if u hav a int table struct then use split like this
SPLIT string AT comma INTO TABLE int_struct
then use this int_struct( here with 128 fields as you said ) for ur calculations
Thanks
Ben
‎2013 Jan 04 6:04 AM
Hi,
I can not specify p1 p2 p3 because first I do not know the Number of fields I'l get and the names of the columns of the table formed since it has been created dynamically.
and can not use SPLIT string AT comma INTO TABLE int_struct since this gives me output.
wa-col1 wa-col2 wa-col3 .....
1
2
3
4
5
6.
Thanks,
Chandni Sharma
‎2013 Jan 04 6:05 AM
If I understood correctly what you said, You meant take the name of the field of the dynamic internal table into a field symbol and assign that component a value from first table.
Please help me get the name of the field of Dynamic internal Table into a field symbol. I am struggling with that only.
Thanks,
Chandni Sharma.
‎2013 Jan 04 6:10 AM
Hi Chandni
Can you try transposing the result of SPLIT into table result in int_struct... .
Just loop through the int_struct and modify the int int_struct with index .... if this satisfy ur requirement...
--- for this you need to have another int tab of same struct and need to modify this intab on looping the original
Thanks Ben
‎2013 Jan 04 6:16 AM
No, I meant take the nth field from first internal table and set it to the nth field from the second internal table. Something like below.
LOOP AT itab1 ASSIGNING <fs_1>.
DO 128 TIMES. " 128 = no. of fields in itab1
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_1> TO <fs_val1>.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_val2>.
<fs_val2> = <fs_val1>.
ENDDO.
APPEND <fs_2> TO itab2.
ENDLOOP.
‎2013 Jan 04 6:27 AM
but the second assign statement gives me sy-subrc 4 and hence no result.
Thanks,
Chandni Sharma.
‎2013 Jan 04 9:20 AM
Hello Chandni,
's solution is the one you have to use. I've used his code as reference:
LOOP AT itab1 ASSIGNING <fs_1>.
WHILE sy-subrc = 0.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_1> TO <fs_val1>.
CHECK sy-subrc = 0. "Field exists in structure <fs_1>
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_val2>.
CHECK sy-subrc = 0. "Field exists in structure <fs_2>
* Only if the field exists in both the structures transfer them
<fs_val2> = <fs_val1>.
ENDWHILE.
APPEND <fs_2> TO itab2.
ENDLOOP.
And are the field types(and length) same for both the internal tables? If so, i don't think you need to write all these lines!
BR,
Suhas
‎2013 Jan 04 6:41 AM
Hi Chandni,
Please post your code as to how you have declared internal table. Why are you trying to move data between internal tables? What's the requirement? Thanks!
Regards,
Kumud
‎2013 Jan 04 6:53 AM
hi,
I have data in Excel file in which I am getting the names of the fields in the first row and data corresponding to that in next few rows. I have to declare an internal table( already done) with the fields specified in first row. The only problem now I am facing is how to move data from next rows to respective columns of the Dynamically Declared table.
itab contains the data from the excel.
read table itab into wa index 1.
a3 = wa-aa.
do.
split a3 at ',' into a1 a2.
w_it-fieldname = a1.
append w_it to it.
i_1-str = a1.
append i_1.
if a1 ne a3.
concatenate a1 ',' into a1.
replace first occurrence of a1 in a3 with space.
condense a3.
else."if a3 is initial.
exit.
endif.
enddo.
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it
importing
ep_table = ep_table.
ep_table is the dynamically created table.
‎2013 Jan 04 8:13 AM
Hi Chandni,
Using RTTI is always preferred way of declaring dynamic internal tables. However, going by the way you have done so far, Kumar Akshat's solution seems right to me. If I have to write the Pseudo code for your requirement it would be like:
Loop at itab from index 2.
Do (no. of fields in itab) times.
*Assign component sy-index of ep_table to <fs_val1>.
*Read the value of first component from itab to <fs_val2>.
*Exchange the values <fs_val1> = <fs_val2>.
Enddo.
*Append the table
Endloop.
‎2013 Jan 04 8:46 AM
Hi Kumud,
as I have mentioned it is not working as I am not getting the component in <fs_val1>. I will read about RTTI to check if that solves my problem.
Thanks,
Chandni Sharma.
‎2013 Jan 04 8:50 AM
‎2013 Jan 04 1:15 PM
HI CHECK IT MIGHT BE USEFUL
PART1:CREATING DYNAMC INTERNAL TABLE HERE U GET ONLY HEADER DONT FORGET THAT
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt2_fcat
IMPORTING
ep_table = lt_dynamic_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
HERE LT2_FCAT CONTAINS THE HEADER LIKE MY HEADER FPC DESCIPTION ...LIKE WISE
TYPE STANDARD TABLE <FS_TABLE>
ASSIGN lt_dynamic_table->* TO <fs_table>.
CREATE DATA ls_line LIKE LINE OF <fs_table>.
ASSIGN ls_line->* TO <fs_struct>.
CREATE DATA ls_temp1 LIKE LINE OF <fs_table>.
ASSIGN ls_temp1->* TO <fs_local>.
PART2:
HERE IT_FINAL HAS DATA THAT I AM PLACING TO RESPECTIVE COLUMNS SEE BELOW HERE I AM MOVING TOTAL DATA INTO <FS_TABLE> IT MIGHT BE CONFUSING PLEASE REPLY FOR ANY DOUBTS
PART2:
EX:IT_FINAL HAS DATA LIKE
SS SAMPLEDESC
DD HIGH DESC ETC
<FS_STRUCT> CONTAINS U R HEADER LIKE
PRODUCT DESCRIPTION LIKE THAT,
FINAL WE WANT TABLE TO SHOW LIKE
FPC DESCRIPTION --->HEADER
SS SAMPLEDESC -->DATA PART
DD HIGH DESC
*ISNT IT
LOOP AT IT_FINAL INTO WA_FINAL.
ASSIGN COMPONENT 'FPC' OF STRUCTURE <fs_struct> TO <fs_temp>.
<fs_temp> = WA_FINAL-PRODUCT.
UNASSIGN <fs_temp>.
ASSIGN COMPONENT 'DESCRIPTION' OF STRUCTURE <fs_struct> TO
<fs_temp>.
<fs_temp> = WA_FINAL-MAT_DESC.
UNASSIGN <fs_temp>.
<fs_temp> = WA_FINAL-QUANTITY.
UNASSIGN <fs_temp>.
CLEAR: <FS_STRUCT>
ENDLOOP.
BY USING ABOVE LOOP WE WILL CHECK EACH COLUMN VALUES SHOULD SIT UNDER THE CORRESPONDING COLUMN ONLY
PART 3:
IF U R PURPOSE IS TO DISPLAY THE ALV LIST IN OUTPUT USE BELOW FOR THAT AFTER GETTING DATA INTO <FS_TABLE>
"******LIST DISPLAY
DATA: lo_alv TYPE REF TO cl_salv_table.
data: gr_columns type ref to cl_salv_columns_table,
l_column type ref to cl_salv_column.
data: lv_value type SCRTEXT_L,
mv_value TYPE SCRTEXT_M,
sv_value TYPE SCRTEXT_S.
field-SYMBOLS: <f_line>, <f_field>.
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
* list_display = 'X'
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = <fs_table> ).
CATCH cx_salv_msg .
ENDTRY.
*** ADDING HEADER
gr_columns = lo_alv->get_columns( ).
gr_columns->set_optimize( 'X' ).
LOOP AT lt2_fcat INTO ls_fcat.
LS_FCAT-JUST = 'C'.
ASSIGN COMPONENT ls_fcat-fieldname OF STRUCTURE <f_line> TO <f_field>
.
try.
l_column = gr_columns->get_column( ls_fcat-fieldname ).
l_column->set_alignment( '3' ).
l_column->set_output_length( '5' ).
clear: lv_value, mv_value, sv_value.
lv_value = ls_fcat-fieldname.
mv_value = ls_fcat-fieldname.
*
sv_value = ls_fcat-fieldname.
l_column->set_long_text( lv_value ).
l_column->set_medium_text( mv_value ).
*
l_column->set_short_text( sv_value ).
catch cx_salv_not_found.
endtry.
ENDLOOP.
****ALV FORMAT
data: lr_functions TYPE REF TO cl_salv_functions_list.
lr_functions = lo_alv->get_functions( ).
lr_functions->set_all('X').
DATA: LO_DISPLAY TYPE REF TO CL_SALV_DISPLAY_SETTINGS.
*
LO_DISPLAY = LO_ALV->GET_DISPLAY_SETTINGS( ).
* set ZEBRA pattern
LO_DISPLAY->SET_STRIPED_PATTERN( 'X' ).
* lo_alv->display( ).
CHECK IT OUT....
PLEASE ASK IF U HAVE ANY CONFUSION, DONT BE DISAPPOINTED WILL DO IT FOR SURE
THANKS
SUDHIR
‎2013 Jan 23 6:15 AM
Hi Sudhir,
ASSIGN COMPONENT 'FPC' OF STRUCTURE <fs_struct> TO <fs_temp>.
<fs_temp> = WA_FINAL-PRODUCT.
UNASSIGN <fs_temp>.
ASSIGN COMPONENT 'DESCRIPTION' OF STRUCTURE <fs_struct> TO
<fs_temp>.
<fs_temp> = WA_FINAL-MAT_DESC.
UNASSIGN <fs_temp>.
<fs_temp> = WA_FINAL-QUANTITY.
UNASSIGN <fs_temp>.
here I do not have the name of the columns, so even this did not work.
Thanks & Regards,
Chandni Sharma
‎2013 Jan 04 7:22 AM
Hi Chandni Sharma,
For your requirement you have to know the concept of RTTI and RTTS
See the links for your requirement
<link farm removed>
Message was edited by: Suhas Saha
‎2013 Jan 04 7:29 AM
As already written perform a LOOP with ASSIGN COMPONENT sy-index, and don't forget to manage with sy-subrc on ASSIGN statements and/ord TRY/CATCH classes like CX_SY_CONVERSION_ERROR/ENDTRY on the MOVE statements when you must exit the loop or raise an error.
LOOP AT itab1 ASSIGNING <struc1>.
APPEND INITIAL LINE TO itab2 ASSIGNING <struc2>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <struc1> TO <field1>.
IF sy-subrc NE 0. EXIT. ENDIF.
ASSIGN COMPONENT sy-index OF STRUCTURE <struc2> TO <field2>.
IF sy-subrc NE 0. EXIT. ENDIF.
TRY.
<field2> = <field1>.
CATCH: cx_sy_conversion_no_number,
cx_sy_conversion_overflow,
cx_sy_move_cast_error.
" Error message ?
ENDTRY.
ENDDO.
ENDLOOP.
Regards,
Raymond