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

How to write duplicate data into error file?

Former Member
0 Likes
1,481

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

10 REPLIES 10
Read only

Former Member
0 Likes
1,297

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.

Read only

0 Likes
1,297

Hi Lakshmi,

I am not so understand on your code.

Can you explain abit more?

Thanks.

Read only

0 Likes
1,297

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

Read only

former_member184635
Participant
0 Likes
1,297

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.

Read only

0 Likes
1,297

Hi Dadarao,

After this how to write the duplicate data into error file?

Thank you.

Read only

Former Member
0 Likes
1,297

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.

Read only

0 Likes
1,297

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?

Read only

0 Likes
1,297

Hi,

Try to do the code suggestion of . Then add the following

  • Check if duplicates_found is not initial.
  • If yes, then display your error message. Get the value of itab2.
  • If no, display your success message.

Regards,

Jake.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,297

You could adapt the following code

SORT itab BY guid.

LOOP AT itab ASSIGNING wa.

  AT NEW guid.

    new_guid_record = abap_true.

  ENDAT.

  IF new_guid_record IS INITIAL.

    APPEND wa TO itab2.

    DELETE itab.

    duplicates_found = abap_true.

  ELSE.
    CLEAR new_guid_record.

  ENDIF.

ENDAT.

Regards,

Raymond

Read only

Former Member
0 Likes
1,297

  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