Skip to Content
author's profile photo Former Member
Former Member

Insert/Update fields into custom table by checking key field

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>.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Nov 14, 2009 at 06:15 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 14, 2009 at 03:05 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      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.

  • Posted on Nov 14, 2009 at 03:32 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 14, 2009 at 03:34 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 14, 2009 at 03:37 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.