Application Development 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: 

JOB and INSERT

former_member204025
Participant
0 Kudos

Hi all again.

I'm practicing JOBS, and I have a program that insert 3 records in a Z table What I do is:

1. Loop to my internal table and then record by record I user the clause INSERT, if sy-subrc eq 0, then COMMIT and write:/ Record added, ELSE. rollback work and write:/ Record not added.

2. When i execute the job via SUBMIT clause, and then I go to the SM37 trx, I can't see the spool list. This happens when I try to add records which were added previously, but if there are new records, I can see the spool list and I don't have problems.

Whys is this happening? I assume that the insert is not possible when the record exist in the Z Table, but I have the rollback and the write, why I can't see the writes?

Can someone explain to me, and let me know how I can solve it?

Thanks

Gaby

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

The insert statement fails if the table already contains a row with the same primary key as that you define in the condition of the insert statement. Since you have written the else part as 'Records not added' , it should have been executed if you were trying to insert the already existing records. Try executing the report in foreground and check the sy-subrc value when you try to insert already available records. Also i dont see the use of rollback here since in the else part no record will be inserted. Remove that also and check

Regards,

Vikranth

7 REPLIES 7

Former Member
0 Kudos

Hi,

The insert statement fails if the table already contains a row with the same primary key as that you define in the condition of the insert statement. Since you have written the else part as 'Records not added' , it should have been executed if you were trying to insert the already existing records. Try executing the report in foreground and check the sy-subrc value when you try to insert already available records. Also i dont see the use of rollback here since in the else part no record will be inserted. Remove that also and check

Regards,

Vikranth

0 Kudos

Hi Vikranth,

I tried deleting the rollback line. And when I use that in a loop works fine, but when I use this line:

INSERT ztabla_vuelos FROM TABLE ti_vuelos.

if there is a duplicate record so then I see the dump, I debugged the code, and when the point is on the INSERT line, after pressing F5 I see the dump error, and tells me that I',m triying to add a duplicate record -which is true- but i don't know how can avoid that, I would like to show a line like this:

INSERT ztabla_vuelos FROM TABLE ti_vuelos.

IF sy-subrc EQ 0.

COMMIT WORK.

WRITE 😕 'OK '.

ELSE.

WRITE: / 'failed'.

ENDIF.

But i can't get it. How can I solve that?

Thanks

0 Kudos

If the primary key set already exist, there is not way u can insert another entry with the same key(set). Modify would look if the key exist, if not it will add a new row else.. it will modify the data releavent to that data (primary key)

modify ztabla_vuelos FROM TABLE ti_vuelos.
IF sy-subrc EQ 0.
COMMIT WORK.
WRITE 😕 'OK '.
ELSE.
WRITE: / 'failed'.
ENDIF.

0 Kudos

I see that. So, I only can use the INSERT clause, when I'm sure that there are no duplicates rows, right? If I'm not sure, I should use Modify clause ?

0 Kudos

>insert: checks duplicate key entry : if duplicate present : fails

>modify: checks duplicate key entry : if duplicate present : modifies, else: inserts.

hope this helps to clarify

0 Kudos

Thanks for your help!! It's really helpful

former_member156446
Active Contributor
0 Kudos

inspite you want the new values to be updated in the Ztable u can use MODIFY statement. Hit an F1 on modify and look for the help on how to use it.