‎2009 Apr 01 1:00 PM
Hi Leads,
I have a small issue when I am inserting data into Database Table.
When I am inserting the signle record into database (using work area), If insert fails..I am able to catch the exeption.
INSERT KNA1 FROM WA_KNA1.
IF SY-SUBRC NE 0.
RAISE EXCEPTION_KNA1.
ENDIF.
But when I am inserting multiple records into database (using internal table), In that case I am unable to catch the exception.
INSERT KNA1 FROM TABLE IT_KNA1.
IF SY-SUBRC NE 0.
RAISE EXCEPTION_KNA1.
ENDIF.
Can you please give me any alternative to catch the exception in this condition.
Regards
Sandip
‎2009 Apr 01 1:05 PM
Hi,
If you use the TABLE options in INSERT statment then if atleast one record got successfully inserted in KNA1 table then SY-SUBRC is set to 0.
You can try this way.
LOOP AT IT_KNA1 INTO WA_KNA1.
INSERT KNA1 FROM WA_KNA1.
IF SY-SUBRC NE 0.
RAISE EXCEPTION_KNA1.
ENDIF.
ENDLOOP.
‎2009 Apr 01 1:08 PM
Use, addition ACCEPTING DUPLICATE KEYS , it will avoid inserting duplicates and returns sy-subrc as 0...
Hope it helps
Regards,
Pavan
‎2009 Apr 01 1:09 PM
You cannot do that if you insert using table statements, you have to do inside loop only. But it will be a performance issue. What you can do is, commit after inserting using tables and get the data from the table and cross check with you entries and populate the records which were not inserted.
‎2009 Apr 01 1:24 PM
hi,
U have to insert all the records of internal table one by one with help of work area.
so use loop.
Regards
‎2009 Apr 01 1:32 PM
U can incorporate this logic
tables: makt.
data: begin of itab occurs 0.
INCLUDE STRUCTURE MAKT.
DATA: end of itab.
data: n type i.
itab-mandt = '800'.
itab-matnr = 'M222'.
itab-spras = 'E'.
itab-maktx = 'M2'.
itab-maktg = 'M2'.
append itab. clear itab.
itab-mandt = '800'.
itab-matnr = 'M223'.
itab-spras = 'E'.
itab-maktx = 'M3'.
itab-maktg = 'M3'.
append itab. clear itab.
itab-mandt = '800'.
itab-matnr = 'M224'.
itab-spras = 'E'.
itab-maktx = 'M4'.
itab-maktg = 'M4'.
append itab. clear itab.
itab-mandt = '800'.
itab-matnr = 'M225'.
itab-spras = 'E'.
itab-maktx = 'M5'.
itab-maktg = 'M5'.
append itab. clear itab.
DESRCIBE TABLE itab lines n.
if not itab[] is initial.
INSERT MAKT FROM TABLE ITAB.
COMMIT WORK.
ENDIF.
if n = sy-dbcnt.
write:/ 'All records inserted'.
else.
write:/ 'Some records missed'.
endif.Mahesh
‎2009 Apr 02 5:34 AM
Hi..
Sort on Primary keys and then use delete adjacent Duplicates on Primary keys .
Or use ACCEPTING DUPLICATE KEYS.
Salil...