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

insert is returning SY-SUBRC = 4.

Former Member
0 Likes
6,873

HI experts,


I've created a Z table for which I'm trying to fill up the entries  by using a FM.
now the problem is that first record is inserted but later on  sy-subrc gives value 4 and no data is inserted.

syntax which I tried .

INSERT ZNWT_INV_ERRLIS1 from table lt_inv_errlist ACCEPTING DUPLICATE KEYS.

insert ZNWT_INV_ERRLIS1 into lwa_INV_ERRLIST values .

I need you help

Thanks in advance.

Regards Suhas.

HI experts,


I've created a Z table for which I'm trying to fill up the entries  by using a FM.
now the problem is that first record is inserted but later on  sy-subrc gives value 4 and no data is inserted.

syntax which I tried .

INSERT ZNWT_INV_ERRLIS1 from table lt_inv_errlist ACCEPTING DUPLICATE KEYS.

insert ZNWT_INV_ERRLIS1 into lwa_INV_ERRLIST values .

I need you help

Thanks in advance.

Regards Suhas.

8 REPLIES 8
Read only

Former Member
0 Likes
3,748

Hi Suhas,

Subrc 4 is returned because you are inserting a record with the same key as the record inserted before.

syntax for db insertion is

INSERT INTO db_tab VALUES wa.

Read only

Former Member
0 Likes
3,748

Hi Suhas,

This is what SAP documentation says about your issue -

" If one or more lines cannot be inserted because the database already contains a line with the same primary key, a runtime error occurs. You can prevent the runtime error occurring by using the addition ACCEPTING DUPLICATE KEYS. In this case, the lines that would otherwise cause runtime errors are discarded, and SY-SUBRC is set to 4."

Source- http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3a6d358411d1829f0000e829fbfe/content.htm

BR.

Read only

0 Likes
3,748

Hi,

Through my previous post what i wanted to convey is that you cannot in any situation insert duplicate keys in Database table but using the ACCEPTING DUPLICATE KEYS clause you can prevent the short dump decently since processor sets sy-subrc to 4 if such syntax is used.

BR.

Read only

Former Member
0 Likes
3,748

There may be two cases where you the insertion can be failed.

Case 1:

A record may already exists in the data base with the same key of the record that you are trying to insert.

To know which record insertion is the problem:

SM13

Give required inputs and Execute.

You will see your update error.

Double click on it and there you can see a red colored row.

Select it and Ctrl+Shft.+F6 to see the tables data which resulted in the update termination.

Case 2 :

The record which you are trying to insert may contain inconsistent data as keys.

Read only

Former Member
0 Likes
3,748

Hi suhas,

The sy-subrc = 4 becoz of the primary key combinations.you cannot insert duplicates entries.

'ACCEPTING DUPLICATE KEYS' is used for avoiding the runtime error only.

using sy-dbcnt you can get the number of records inserted.

Cheers

Priya.

Read only

Former Member
0 Likes
3,748

Hi Suhas,

You are trying to insert the record with the same key values, so you are getting sy-subrc value 4 and not able to insert the values into database table. Change the key values and try inserting to database it will insert successfully.

Read only

Former Member
0 Likes
3,748

Hey,

the correct syntax to insert data into table is:

 

INSERT { {INTO target VALUES source }

       | {     target FROM   source } }.

You are trying to insert duplicate record with same value of primary key, which is not possible to insert as the database already has the same record. Change the primary key value and then try to insert.

hope it helps!

Read only

Former Member
0 Likes
3,748

Hi,

make sure to have cleared the internal table lt_inv_errlist bewtween to INSERTs with REFRESH (or CLEAR, if it has no header line).

You can sort the internal table and remove  duplicates with DELETE ADJACENT DUPLICATES to avoid duplicates during one INSERT of several entries. Refer to the key fields of your Z-table.

Use MODIFY instead of INSERT, if you can't avoid duplicates because of data selection reasons.

Regards,

Klaus