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: 

Insert/Update fields into custom table by checking key field

Former Member
0 Kudos

Hi,

I have to two tables like Dept in which fields are

DEPT

===========

CLNT

DEPTID( PK )

,DEPTNAME.

Another table is EMP, the fields are

EMP

============

CLNT

EMPID(PK)

DEPTID(PK)

EMPNAME.

In this created a foreign key relation ship withEMP-DEPTID = DEPt-Deptid.

In program

I want to insert some records to my custom table EMP.

There It should accept only emp-deptid value if it exists in Dept-deptid.

what statement I should write.

When I write " insert into zEMP values <workarea>.

6 REPLIES 6

nirajgadre
Active Contributor
0 Kudos

Hi,

Get the data of your table DEPT into internal table.

while inserting the data check if the work area Deptid id is present in the internal table of DEPT.

if yes then insert the record into the EMP table.

Former Member
0 Kudos

Hi,

This is very simple...

1.Just before the insert statement READ TABLE DEPT WITH DEPTid = DEPTID.

2. check for sy-subrc value for 0

3.if yes then insert else prompt the erroe message or do nothing depending upon the requirement.

Hope this will solve your problem.

pooja

0 Kudos

Hi Pooja,

I am not in front of the SAP Login now. So, can't confirm.

Doesn't the table throw an error when there is a foreign key violation??

I didn't know we were supposed to write a check explicitly !!!

@OP

I am not too sure about this. Probably you can check for yourself.

kesavadas_thekkillath
Active Contributor
0 Kudos

Its better that you create a foreign key relationship between these two tables.

Are you using table maintenance to insert data ?

if its from your program just use modify table statement

Check F1 help documentation for modify table

Former Member
0 Kudos

Hi Bachi,

Check the existance of record in foregin table and then only insert the record.

or else.

first insert in the foregin key table and then insert your table.

Hope you get it.

kesavadas_thekkillath
Active Contributor
0 Kudos

Are you using table maintenance to insert data ?

before table updation just put a query

like

select single * from table1 where empid =empid.

if sy-subrc = 0.

"True

else.

"false.

endif.

or

You can try function module HELP_VALUES_GET_WITH_CHECKTAB

Also Check F1 help documentation for modify table