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

SUBRC during mass insertion

Former Member
0 Likes
2,311

Experts,

I am inserting multiple records into database as below,

INSERT DFKKOP FROM TABLE IT_DFKKOP.

Requirement is to capture SY-SUBRC for each record & display it to user(Note: If insert fails, then user wants to take action manually for the same)

One option, that I can think of is to insert 1 record at a time in Loop as below,

Loop at IT_DFKKOP into WA_DFKKOP.

Insert DFKKOP from WA_DFKKOP.

if sy-subrc ne 0.

----> " ls_errorinfo-message = 'Insertion Failed'.

append ls_errorinfo to lt_errorinfo.

else.

----> " ls_errorinfo-message = 'Inserted Successfully'.

append ls_errorinfo to lt_errorinfo.

endif.

Endloop.

Kindly guide me if there is a better efficient option that the above.

Regards,

Aspire

1 ACCEPTED SOLUTION
Read only

palash_mazumder
Participant
0 Likes
2,254

Hi,

You can capture all the data into the internal table and

You should always use the lock object,

below is the code

if li_DFKKOP  is not initial.

    call function 'ENQUEUE_E_TABLEE'

      exporting

        mode_rstable   = 'E'

        tabname        = 'DFKKOP'

      exceptions

        foreign_lock   = 1

        system_failure = 2

        others         = 3.

    .

   modify DFKKOP  from table li_DFKKOP.

    call function 'DEQUEUE_E_TABLEE'

      exporting

        mode_rstable = 'E'

        tabname      = 'DFKKOP'.

   

  endif.

Experts,

I am inserting multiple records into database as below,

INSERT DFKKOP FROM TABLE IT_DFKKOP.

Requirement is to capture SY-SUBRC for each record & display it to user(Note: If insert fails, then user wants to take action manually for the same)

One option, that I can think of is to insert 1 record at a time in Loop as below,

Loop at IT_DFKKOP into WA_DFKKOP.

Insert DFKKOP from WA_DFKKOP.

if sy-subrc ne 0.

----> " ls_errorinfo-message = 'Insertion Failed'.

append ls_errorinfo to lt_errorinfo.

else.

----> " ls_errorinfo-message = 'Inserted Successfully'.

append ls_errorinfo to lt_errorinfo.

endif.

Endloop.

Kindly guide me if there is a better efficient option that the above.

Regards,

Aspire

17 REPLIES 17
Read only

vijay_hariharan
Contributor
0 Likes
2,254

Hi Aspire,

Your method seems alright since you require SUBRC value for each record, but just a disclaimer, in case the Insertion leads to duplicates, the System will dump. If you are sure there won't be Duplicates, you can continue using INSERT else if there are records with same key field values but varied non-key field values, you can use MODIFY statement.

Hope this helps.

Regards,

Vijay

Read only

0 Likes
2,254
Thanks a lot for your reply Vijay.  I am interested to hear the viewpoints of Other experts too.   Kindly share your valuable suggestions.
Read only

Former Member
0 Likes
2,254

Hi Aspire,

I only have a point need share with you.

if the size not very big for DFKKOP, you can get data from DFKKOP again after insert or modify it.

   SELECT OPBEL
       OPUPW
       OPUPK
       OPUPZ
  FROM DFKKOP
  INTO TABLE LT_DFKKOP
   FOR ALL ENTRIES IN IT_DFKKOP
WHERE OPBEL = IT_DFKKOP-OPBEL
   AND OPUPW = IT_DFKKOP-OPUPW
   AND OPUPK = IT_DFKKOP-OPUPK
   AND OPUPZ = IT_DFKKOP-OPUPZ.

and now LT_DFKKOP it's successful table.

if you only need success data, then don't need LOOP.

Thinks more...

Read only

0 Likes
2,254

Dear Chang,

Thanks a lot for your reply. In my case I want success or error message for every single record. I have implemented the solution of loop thru my records & checking subrc for every single record for which I have shared the code in above thread.

Good Day!

Aspire

Read only

former_member209120
Active Contributor
0 Likes
2,254

Hi Aspire WF,

Your code is ok.... you can proceed with this code...

Loop at IT_DFKKOP into WA_DFKKOP.

Insert DFKKOP from WA_DFKKOP.

if sy-subrc ne 0.

append ls_errorinfo to lt_errorinfo.

else.

append ls_errorinfo to lt_errorinfo.

endif.

clear WA_DFKKOP.

Endloop.

Read only

Arun_Prabhu_K
Active Contributor
0 Likes
2,254

Hi.

As per my knowledge, one case where INSERT fails is when record with same primary key(s) is already existing in the table.

My suggestion is

select * from DFKKOP into table tmp_dfkkop for all entries in it_dfkkop

where primary key(s) = it_dfkkop-primary_key(s).

Now display these records for the end-user to take action.

Regards.

Read only

0 Likes
2,254

Arun & Ramesh,

Thanks a lot for your replies. As per all your suggestions, I have replaced insert with MODIFY statement. My code looks as below,

Loop at IT_DFKKOP assigning <FS_DFKKOP>.

WA_DFKKOP = <FS_DFKKOP>. " Work area to store old record

<FS_DFKKOP>-XXX = 'New Value'....

Modify DFKKOP from <FS_DFKKOP>.

IF SY-SUBRC EQ 0.

Ls_errorinfo-message = 'Update successful'.

append ls_errorinfo to lt_errorinfo.

Delete DFKKOP from WA_DFKKOP. " Delete Old record as modify statement in my case inserts a new record because I am changing the key field in DFKKOP

else.

Ls_errorinfo-message = 'Update Failed'.

append ls_errorinfo to lt_errorinfo.

endif.

Endloop.

Thanks,

Aspire

Read only

palash_mazumder
Participant
0 Likes
2,255

Hi,

You can capture all the data into the internal table and

You should always use the lock object,

below is the code

if li_DFKKOP  is not initial.

    call function 'ENQUEUE_E_TABLEE'

      exporting

        mode_rstable   = 'E'

        tabname        = 'DFKKOP'

      exceptions

        foreign_lock   = 1

        system_failure = 2

        others         = 3.

    .

   modify DFKKOP  from table li_DFKKOP.

    call function 'DEQUEUE_E_TABLEE'

      exporting

        mode_rstable = 'E'

        tabname      = 'DFKKOP'.

   

  endif.

Read only

0 Likes
2,254

Palash,

Thanks a lot for your reply. Is it mandatory to lock the table before modifying or inserting records. All these days I have been modifying without this extra check except when I update details for an employee using HR_INFOTYPE_OPERATION

Thanks

Aspire

Read only

0 Likes
2,254

It is not mandatory but just common sense.

And why are you writing data to a SAP standard table direcly via insert/modify?

Sougata.

Read only

0 Likes
2,254

Hi

Sougata It should be common practice to use lock object.Using lock object data will be consistent and this is rdms rule...

Read only

0 Likes
2,254

thanks Aspire,,,,,

Read only

0 Likes
2,254

Palash,

I did not say its not a common practice to lock/unlock before/after database update - On the contrary, I pointed out  that common sense should prevail before and after a programmatic database update. Lock and unlock are not mandatory but they are just good practices - programs still compile correctly if someone did not lock/unlock before database updates...hence it is not "mandatory".

But I still do not understand why a SAP standard table is directly written to via direct insert/modify statements. Is that a "common practice" too?

Sougata.

Read only

0 Likes
2,254

Sougata,

Thanks a lot for all your replies.

Actually I am trying to update DFKKLOCKS table & not DFKKOP as mentioned in this thread. I tried couple of standard FMs like Z_CFI_FKK_SAMPLE_1801 & BAPI_CTRACCONTRACTACCOUNT_CHNG to update lock records in DFKKLOCKS table. But,

Z_CFI_FKK_SAMPLE_1801 - Tried with a test program but is not updating the records correctly

BAPI_CTRACCONTRACTACCOUNT_CHNG - Obsolete FM so had to drop this FM

As a last option, I decided to update the standard table directly.

Thanks,

Aspire


Read only

0 Likes
2,254

Aspire,

Have you looked at the functions available under Function Group FKLOCK? Have you analysed the Function Module FKK_O_LOCK_METHOD_SAVE?

Sougata.

Read only

0 Likes
2,254

Sougata,

Thanks for your reply. I have not looked at it. Will check them now & keep you posted.

Good Day..!

Thanks,

Aspire

Read only

0 Likes
2,254

Sougata,

I used FM FKK_O_LOCK_METHOD_SAVE to insert a new record in DFKKLOCKS table. Since i am changing one of the key fields(TDATE), always it inserted a new record rather than updating existing record. So if insert was successful, I deleted the existing record using FKK_S_LOCK_DELETE.

Thanks a lot everyone for all your valuable inputs.

Aspire