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

Regarding Insert

Former Member
0 Likes
746

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

6 REPLIES 6
Read only

Former Member
0 Likes
726

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.

Read only

Former Member
0 Likes
726

Use, addition ACCEPTING DUPLICATE KEYS , it will avoid inserting duplicates and returns sy-subrc as 0...

Hope it helps

Regards,

Pavan

Read only

Former Member
0 Likes
726

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.

Read only

Former Member
0 Likes
726

hi,

U have to insert all the records of internal table one by one with help of work area.

so use loop.

Regards

Read only

former_member222860
Active Contributor
0 Likes
726

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

Read only

Former Member
0 Likes
726

Hi..

Sort on Primary keys and then use delete adjacent Duplicates on Primary keys .

Or use ACCEPTING DUPLICATE KEYS.

Salil...