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 in the custom database table to create a new entry.

former_member288488
Participant
0 Likes
2,624

Hello Experts,

I'm trying to INSERT the entry in my custom databse table that i have created. But I'm not sure what i'm missing as i'm getting the sy-subrc = 4 after the INSERT statement and my records are not added to the database table.

I'm using the INSERT as below:-

APPEND gw_relax TO gt_relax.

INSERT INTO zmm_bg_relax VALUES gw_relax.

Please let me know your helpful responses on this,

Thanks,

Naveen

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,458

Hello Naveen

sy-subrc = 4 usually means the record already exists in the DB table.

In order to check this you may add the option ...ACCEPTING DUPLICATE KEYS to the INSERT statement.

Regards

Uwe

8 REPLIES 8
Read only

syed_ibrahim5
Active Participant
0 Likes
1,458

hi,

please check the primary key values in the db table and the values in the internal table.... if the entries of the primary keys inserted from the internal table already exists then it might not get inserted ...

with regards,

syed

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,459

Hello Naveen

sy-subrc = 4 usually means the record already exists in the DB table.

In order to check this you may add the option ...ACCEPTING DUPLICATE KEYS to the INSERT statement.

Regards

Uwe

Read only

0 Likes
1,458

Hi Uwe,

Now i have modified my statement as INSERT zmm_bg_relax FROM TABLE gt_relax ACCEPTING DUPLICATE KEYS.

But still i'm getting sy-subrc = 4 .

Thanks,

Naveen

Read only

0 Likes
1,458

Check [INSERT dbtab|http://help.sap.com/abapdocu_70/en/ABAPINSERT_SOURCE.htm] [FROM TABLE itab [ACCEPTING DUPLICATE KEYS]|http://help.sap.com/abapdocu_70/en/ABAPINSERT_SOURCE.htm#&ABAP_ALTERNATIVE_2@2@],

If the addition ACCEPTING DUPLICATE KEYS is specified, all rows are inserted for which this is possible. The remaining rows are rejected and sy-subrc is set to 4. The system field sy-dbcnt is set to the number of lines that are inserted.

So if you get sy-subrc = 4, some of the records of the internal table are already contained in the database table.

You cannot put 2 or more records in a database table with the same primary key, primary keys are unique keys with the addition of a not-null constraint. If you need multiple records with a same key, you must add another key to the primary key. (use a timestamp or a counter)

Regards,

Raymond

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,458

If you try to INSERT rows using the same PRIMARY key then a treatable runtime exception CX_SY_OPEN_SQL_DB gets raised if you're not using ACCEPTING DUPLICATE KEYS.

SAP documentation: [http://help.sap.com/abapdocu_70/en/ABAPINSERT_SOURCE.htm#&ABAP_ALTERNATIVE_2@2@]

BR,

Suhas

Read only

Former Member
0 Likes
1,458

HI,

try to fill your work area with the new entry you wanted to insert in the Ztable..

snippet code.

DATA : it_tab type standard table of Ztable with header line "mention your ztable name in place of ztable.

It_tab-name = 'ABC'.

it_tab-class='A'.

Insert Ztable from it_tab. " if you want to insert one recoed in to ztable

other wise...

It_tab-name = 'ABC'.

it_tab-class='A'.

append it_tab.

It_tab-name = 'ABC'.

it_tab-class='A'.

append it_tab.

Insert Ztable from table it_tab ' for multiple record..

hope this will help you.

Regards,

Kiran

Read only

Former Member
0 Likes
1,458

HI,

try to fill your work area with the new entry you wanted to insert in the Ztable..

snippet code.

DATA : it_tab type standard table of Ztable with header line "mention your ztable name in place of ztable.

It_tab-name = 'ABC'.

it_tab-class='A'.

Insert Ztable from it_tab. " if you want to insert one recoed in to ztable

other wise...

It_tab-name = 'ABC'.

it_tab-class='A'.

append it_tab.

It_tab-name = 'ABC'.

it_tab-class='A'.

append it_tab.

Insert Ztable from table it_tab ' for multiple record..

hope this will help you.

Regards,

Kiran

Read only

Former Member
0 Likes
1,458

Hello Naveen,

Use MODIFY statement instead of INSERT. MODIFY will either modify the existing record or create a new record.

Cheers,

Vasanth M