Application Development 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: 

How to traverse and check for values in internal table that already exist in the database?

0 Kudos

I am learning ABAP. I need to write a report program to display an error message if I am entering values from internal table that are already there in the database.

I have an internal table: i_tab that has records uploaded from a text file. It might also have some duplicate entries that are already in the database table: ztable. Now I need to insert these entries into the database. If all the entries are new, the program will simply add the new records to the database table ztable and display a success message "Successfully added". However, if there are duplicate entries in the internal table, it should display the erorr message "Duplicate entries". Let us say that there are 2 columns in the i_tab and ztable. Those are user_id and user_name. User_id is the primary key

How do I check for the same entires of i_tab with ztable and display the erorr message?

One way that I tried to do and did not work was :

Here in the below code the loop doesn't work and goes straight to insert table statement so it tries to append the duplicate values and then the program throws a runtime error and does not execute. If it worked, then it would display the error message and not move to the insert statement.

loop at i_tab into wa_tab.
    if ztable-user_id = wa_tab-user_id
       MESSAGE 'DUPLICATE ENTRIES' TYPE 'E'.
    endif.
endloop.

insert ztable from table i_tab.
if sy-subrc = 0.
    MESSAGE 'SUCCESSFULLY ADDED' TYPE 'S'.
endif.
2 REPLIES 2

jerryjanda
Community Manager
Community Manager

Welcome to the SAP Community! Thank you for visiting us to get answers to your questions.

Since you're asking a question here for the first time, I'd like to offer some friendly advice on how to get the most out of your community membership and experience.

First, please see https://community.sap.com/resources/questions-and-answers, as this resource page provides tips for preparing questions that draw responses from our members. Second, feel free to take our Q&A tutorial at https://developers.sap.com/tutorials/community-qa.html, as that will help you when submitting questions to the community.

I also recommend that you include a profile picture. By personalizing your profile, you encourage readers to respond: https://developers.sap.com/tutorials/community-profile.html.

I hope you find this advice useful, and we're happy to have you as part of SAP Community!

All the best,

--Jerry

Moderation Lead

Make sure to subscribe to What's New!

Sandra_Rossi
Active Contributor

If you use

INSERT ... FROM TABLE ... ACCEPTING DUPLICATE ENTRIES.

it will insert lines which are not duplicates, and it will return SY-SUBRC = 4 if there is at least one duplicate. See ABAP documentation for more information.