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: 

CHECKING FOR EXISTING FIELD IN TABLE BEFORE INSERTING NEW DATA

muzizimele
Explorer
0 Kudos
760

I want to check if a record already exist in a table ( zcustomer ) by using it's primary key ( zzcustomer_no) before inserting a new record from the structure table ls_customer to actual table zcustomer. how do i do that?

1 ACCEPTED SOLUTION

shantraj
Explorer
0 Kudos
690

you can use modify statement, which will internally check the entries before inserting into custom table zcustomer.

Modify zcustomer from ls_customer.
4 REPLIES 4

akmal1216
Employee
Employee
690

Hello,

You can use the code below:

SELECT SINGLE @abap_true
       FROM scarr
       WHERE carrid = @carrier
       INTO @DATA(exists).

IF exists = abap_true.
  cl_demo_output=>display( |Carrier { carrier } exists in SCARR| ).
ELSE.
  cl_demo_output=>display( |Carrier { carrier } does not exist in SCARR| ).
ENDIF.

You can also check the following blog from keller.m - the ‘X’ in the select statement

raymond_giuseppi
Active Contributor
0 Kudos
690

Just read any already existing record with a simple SELECT statement. (or use a dirty ACCEPTING DUPLICATE KEYS or a dirty MODIFY)

But first check duplicate records in the internal table.

shantraj
Explorer
0 Kudos
691

you can use modify statement, which will internally check the entries before inserting into custom table zcustomer.

Modify zcustomer from ls_customer.

VXLozano
Active Contributor
0 Kudos
690

The main question is...?

If you need to check if a record exists, the SELECT SINGLE option (as akmal1216 suggested) is the way to go. It's irrelevant WHY you want to check it.

If you need to check it because it's giving you an error while INSERTing, then you must think your process' requirements. What the customer asked for? If you don't care about the existing data, go for a MODIFY (as shantraj said), but it's a very dirty thing to do (as raymond.giuseppi points).

But the main problem here is your question.

And I will not point at the use of CAPS in the subject...