2012 Apr 26 8:02 AM
Hi Experts,
I have below code to check if any duplicate data:
SORT <ft_csv_data> BY guid.
lt_contacts = <ft_csv_data>.
v1 = lines( lt_contacts ).
DELETE ADJACENT DUPLICATES FROM <ft_csv_data> COMPARING guid.
v2 = lines( <ft_csv_data> ).
if v1 NE v2.
MESSAGE: 'Duplicate data exist!' TYPE 'I'.
ENDIF.
If there is duplicate data in internal table, i would want to write the duplicate records into error text file.
Do anyone has idea how to did that?
Thank you.
Regards,
Honda
2012 Apr 26 8:14 AM
Hi,
In order to populate the duplicate records into error file, please try this one.
loop at it_final. "final table
if ls_final = lt_final. "here give the key fields which needs to be compared.
"populate record into erro table
else.
ls_final = lt_final.
endif.
clear lt_final.
endloop.
2012 Apr 26 8:26 AM
Hi Lakshmi,
I am not so understand on your code.
Can you explain abit more?
Thanks.
2012 Apr 26 8:32 AM
Hey,
okey..what we can do is, just compare the current record with the previous one. if both are same then populate the record to error table. thats it..
Here ls_final is the structure which will hold the previous record. just declare it with the same type of it_final.
loop at it_final. "final table
if ls_final = lt_final. "here give the key fields which needs to be compared.
"populate record into erro table
else.
ls_final = lt_final. "Populating the current record into ls_final for back up.
endif.
clear lt_final.
endloop.
let me know if its not clear..
Thanks,
Lakshmi
2012 Apr 26 8:36 AM
Hi Kelvin,
The code is simple, no need to use so much variables.
Just use the below code:
SORT <ft_csv_data> BY guid.
lt_contacts = <ft_csv_data>.
DELETE ADJACENT DUPLICATES FROM lt_contacts COMPARING guid.
if sy-tfill is not initial.
Message 'Duplicate data exist' type I.
endif.
regards,
Dadarao.
2012 Apr 26 8:42 AM
Hi Dadarao,
After this how to write the duplicate data into error file?
Thank you.
2012 Apr 26 8:42 AM
Hi,
You can try the follwoing:
data: lv_flag type flag.
lt_contact_temp[] = lt_contacts[].
sort lt_contacts by guid.
sort lt_contacts_temp by guid.
delete adjacent duplicates from lt_contacts_temp comparing guid.
clear: lw_contacts.
loop at lt_contacts into lw_contacts.
clear: lw_contact_temp.
read table lt_contacts_temp into lw_contacts_temp with key guid = lw_contacts-guid binary search.
if sy-subrc eq 0.
lv_flag = 'X'.
if lv_flag is initial.
move-corresponding lw_contacts to lw_error.
append lw_error to li_error.
clear:lw_error,lv_flag.
endif.
endif.
clear: lw_contacts.
endloop.
Hope this will solve your probelm.
2012 Apr 26 11:12 AM
Hi Prasad,
I have solve the issue with below code:
lt_contacts = <ft_csv_data>.
SORT <ft_csv_data> BY guid.
SORT lt_contacts BY guid.
v1 = lines( lt_contacts ).
DELETE ADJACENT DUPLICATES FROM <ft_csv_data> COMPARING guid.
v2 = lines( <ft_csv_data> ).
LOOP AT <ft_csv_data> INTO ls_contacts_temp.
ENDLOOP.
IF v1 NE v2.
MESSAGE: 'Duplicate data exist!' TYPE 'I'.
LOOP AT lt_contacts INTO ls_contacts WHERE guid NE ls_contacts_temp-guid.
CONCATENATE 'Record no: ' ls_contacts-client 'contain duplicate data at field <GUID>.'
'Duplicate data as:' ls_contacts-guid INTO l_error SEPARATED BY space.
wa_error-line = l_error.
APPEND wa_error TO gt_error.
ENDLOOP.
PERFORM error_file.
ELSE.
MESSAGE: 'No duplicate data!' TYPE 'I'.
ENDIF.
Its working, but not the best method i think. Any best suggestion?
2012 Apr 26 11:22 AM
2012 Apr 26 8:45 AM
2012 Apr 26 11:52 AM
Hi Alexender,
following is the entire code you require:
TYPES: BEGIN OF ty_test,
field1(3),
field2(3),
field3(3),
END OF ty_test.
DATA: it_test TYPE STANDARD TABLE OF ty_test WITH HEADER LINE,
old_f1(3),
old_f3(3),
g_error_file TYPE string,
test_string TYPE string,
c_log_path TYPE filepath-pathintern VALUE 'ZOUT',
g_v_uxdir_xml TYPE rlgrap-filename.
it_test-field1 = 'AAA'. it_test-field2 = 'AAA'. it_test-field3 = 'DDD'. APPEND it_test.
it_test-field1 = 'BBB'. it_test-field2 = 'AAA'. it_test-field3 = 'CCC'. APPEND it_test.
it_test-field1 = 'AAA'. it_test-field2 = 'AAA'. it_test-field3 = 'AAA'. APPEND it_test.
it_test-field1 = 'BBB'. it_test-field2 = 'AAA'. it_test-field3 = 'CCC'. APPEND it_test.
SORT: it_test BY field1 field3.
CALL FUNCTION 'ZFILE_GET_PATH_NAME'
EXPORTING
logical_path = c_log_path
IMPORTING
file_name_path = g_v_uxdir_xml
EXCEPTIONS
path_not_found = 1
missing_parameter = 2
operating_system_not_found = 3
file_system_not_found = 4
OTHERS = 5.
CLEAR g_error_file.
CONCATENATE g_v_uxdir_xml 'test_error_file.txt' INTO g_error_file.
OPEN DATASET g_error_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
LOOP AT it_test.
IF old_f1 = it_test-field1 AND old_f3 = it_test-field3.
CLEAR test_string.
CONCATENATE test_string it_test-field1 it_test-field2 it_test-field3 INTO test_string SEPARATED BY ' '.
TRANSFER test_string TO g_error_file.
ENDIF.
*endloop.
old_f1 = it_test-field1.
old_f3 = it_test-field3.
ENDLOOP.
* Close the dataset
CLOSE DATASET g_error_file.
IF sy-subrc <> 0.
WRITE : 'Error closing Error file'.
ELSE.
WRITE: 'error file created'.
ENDIF.
Thanks and regards,
Aastha