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: 

Inserting records into Custom table

Former Member
0 Kudos

Hi Friends,

I have created a custom table without table maintainence. I need to insert records into this custom table using abap coding only. When a run a download program an excel file is generated. from that excel file i have to summarise the data and upload the summary to Ztable.

Problem is am getting 3 records in summary but only one record is updated into Ztable. Some times total 3 records updated. i dont understand this strange behaviour.

FORM f_update_report_log.

  • Report log: sum of Document currency Amt & local currency Amt

  • for all document currency keys

SORT it_data BY waers.

LOOP AT it_data.

ON CHANGE OF it_data-waers.

IF sy-tabix NE 1.

zdmaplog-dmdate = sy-datum.

zdmaplog-dmtime = sy-uzeit.

zdmaplog-dmtyp = 'D'.

zdmaplog-sbukrs = p_bukrs.

zdmaplog-tbukrs = p_bukrst.

zdmaplog-items = v_lines.

zdmaplog-wrbtr = v_wrbtr.

zdmaplog-dwaers = v_waers.

zdmaplog-dmbtr = v_dmbtr.

zdmaplog-lwaers = 'SGD'.

zdmaplog-userid = sy-uname.

  • update db table zdmaplog

INSERT zdmaplog.

v_subrc = sy-subrc.

COMMIT WORK AND WAIT.

IF v_subrc NE 0.

WRITE: / v_waers, 'Zdmaplog not updated'.

ENDIF.

CLEAR: v_lines, v_wrbtr, v_dmbtr, v_subrc.

ENDIF.

ENDON.

v_waers = it_data-waers.

v_lines = v_lines + 1.

IF it_data-shkzg = 'H'. "credit

v_wrbtr = v_wrbtr - it_data-wrbtr.

v_dmbtr = v_dmbtr - it_data-dmbtr.

ELSEIF it_data-shkzg = 'S'. "Debit

v_wrbtr = v_wrbtr + it_data-wrbtr.

v_dmbtr = v_dmbtr + it_data-dmbtr.

ENDIF.

ENDLOOP.

  • update DB table zdmaplog

zdmaplog-dmdate = sy-datum.

zdmaplog-dmtime = sy-uzeit.

zdmaplog-dmtyp = 'D'.

zdmaplog-sbukrs = p_bukrs.

zdmaplog-tbukrs = p_bukrst.

zdmaplog-items = v_lines.

zdmaplog-wrbtr = v_wrbtr.

zdmaplog-dwaers = v_waers.

zdmaplog-dmbtr = v_dmbtr.

zdmaplog-lwaers = 'SGD'.

zdmaplog-userid = sy-uname.

INSERT zdmaplog.

v_subrc = sy-subrc.

COMMIT WORK AND WAIT.

IF v_subrc NE 0.

WRITE: / v_waers, 'Zdmaplog not updated'.

ENDIF.

ENDFORM. " f_report_log

Its urgent, please help me. i wil reward good points.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hey Fren,

Try this....

FORM f_update_report_log.

  • Report log: sum of Document currency Amt & local currency Amt

  • for all document currency keys

SORT it_data BY waers.

LOOP AT it_data.

AT NEW it_data-waers.

IF sy-tabix NE 1.

zdmaplog-dmdate = sy-datum.

zdmaplog-dmtime = sy-uzeit.

zdmaplog-dmtyp = 'D'.

zdmaplog-sbukrs = p_bukrs.

zdmaplog-tbukrs = p_bukrst.

zdmaplog-items = v_lines.

zdmaplog-wrbtr = v_wrbtr.

zdmaplog-dwaers = v_waers.

zdmaplog-dmbtr = v_dmbtr.

zdmaplog-lwaers = 'SGD'.

zdmaplog-userid = sy-uname.

  • update db table zdmaplog

MODIFY zdmaplog from zdmaplog.

v_subrc = sy-subrc.

COMMIT WORK AND WAIT.

IF v_subrc NE 0.

WRITE: / v_waers, 'Zdmaplog not updated'.

ENDIF.

CLEAR: v_lines, v_wrbtr, v_dmbtr, v_subrc.

ENDIF.

ENDAT.

v_waers = it_data-waers.

v_lines = v_lines + 1.

IF it_data-shkzg = 'H'. "credit

v_wrbtr = v_wrbtr - it_data-wrbtr.

v_dmbtr = v_dmbtr - it_data-dmbtr.

ELSEIF it_data-shkzg = 'S'. "Debit

v_wrbtr = v_wrbtr + it_data-wrbtr.

v_dmbtr = v_dmbtr + it_data-dmbtr.

ENDIF.

ENDLOOP.

On Change of and INSERT dbtab are obsolete statements...

.

Please dont use it in ur Reports.....

Thank you..

Regards,

Abhinay

7 REPLIES 7

Former Member
0 Kudos

Hey Fren,

Try this....

FORM f_update_report_log.

  • Report log: sum of Document currency Amt & local currency Amt

  • for all document currency keys

SORT it_data BY waers.

LOOP AT it_data.

AT NEW it_data-waers.

IF sy-tabix NE 1.

zdmaplog-dmdate = sy-datum.

zdmaplog-dmtime = sy-uzeit.

zdmaplog-dmtyp = 'D'.

zdmaplog-sbukrs = p_bukrs.

zdmaplog-tbukrs = p_bukrst.

zdmaplog-items = v_lines.

zdmaplog-wrbtr = v_wrbtr.

zdmaplog-dwaers = v_waers.

zdmaplog-dmbtr = v_dmbtr.

zdmaplog-lwaers = 'SGD'.

zdmaplog-userid = sy-uname.

  • update db table zdmaplog

MODIFY zdmaplog from zdmaplog.

v_subrc = sy-subrc.

COMMIT WORK AND WAIT.

IF v_subrc NE 0.

WRITE: / v_waers, 'Zdmaplog not updated'.

ENDIF.

CLEAR: v_lines, v_wrbtr, v_dmbtr, v_subrc.

ENDIF.

ENDAT.

v_waers = it_data-waers.

v_lines = v_lines + 1.

IF it_data-shkzg = 'H'. "credit

v_wrbtr = v_wrbtr - it_data-wrbtr.

v_dmbtr = v_dmbtr - it_data-dmbtr.

ELSEIF it_data-shkzg = 'S'. "Debit

v_wrbtr = v_wrbtr + it_data-wrbtr.

v_dmbtr = v_dmbtr + it_data-dmbtr.

ENDIF.

ENDLOOP.

On Change of and INSERT dbtab are obsolete statements...

.

Please dont use it in ur Reports.....

Thank you..

Regards,

Abhinay

0 Kudos

Thanks for response, But the problem is its Control break statements At new, At end are not working. For which types fields we can use Control break statements in side loop?

The problem with Insert is solved that is because of key fields in table definition.

Plz give the answer for Control break statements. I will reward full points.

Thanks.

0 Kudos

Hi Pavan,

For working with control break statements like AT NEW, the field upon which u r working, should be the first field in that internal table.Otherwise it won't give the appropriate results.

In ur case, waers is not the first field in that IT, i suppose.

Usually, we should have the field (which we r working with) as the first field in that structure & also the IT should be sorted by that field, prior to the operations with the control break statements (such as AT NEW).

If u really want to work with those controlbreak statements, u create another IT with WEARS as the first field,sort it by WEARS, run a loop on that & use whatever commands u want. This time it surely works.

Try this & let me know in case of any issues.

Dont forget to reward points,if this is useful.

Pavan.

0 Kudos

Hi,

While using AT NEW and AT END the field you are mentioning should be the first field in the table also do the same in the table declaration try this it may work out. and most importantly sort by the same field.

Thanks and Regards,

Sai

0 Kudos

Hey Fren,

Thank you for rewarding....and ofcourse inspiring...

So,

Regarding usage of Control Break Statements..

Its like this..

SAP stores data internally like the Array.

for example...

AT NEW it_data-waers.
zdmaplog-dmdate = sy-datum.
zdmaplog-dmtime = sy-uzeit.
zdmaplog-dmtyp = 'D'.
zdmaplog-sbukrs = p_bukrs.
zdmaplog-tbukrs = p_bukrst.
zdmaplog-items = v_lines.
zdmaplog-wrbtr = v_wrbtr.
zdmaplog-dwaers = v_waers.
zdmaplog-dmbtr = v_dmbtr.
zdmaplog-lwaers = 'SGD'.
zdmaplog-userid = sy-uname.
ENDAT.

will get stored in single array format internally...

In the above case,

the data will be stored as follows,

-dmdate-dmtime-dmtyp-sbukrs-tbukrs-items-wrbtr-dwaers-dmbtr-lwaers-userid-

here, the data is stored in digital format... so

AT NEW statement will compare the previous Array with the current record....

in this case if u give

AT NEW dmdate

....

it'll check for the change in ' dmdate ' field....and proceed as per the logic....

Not necessary to be the first field...if u want the data in this format..

1 Table_1-Field_A Table_1-Field_B.

__________________________1 Table_2-Field_A Table_2-Field_B

__________________________2 Table_2-Field_A Table_2-Field_B

__________________________3 Table_2-Field_A Table_2-Field_B

__________________________4 Table_2-Field_A Table_2-Field_B

2 Table_1-Field_A Table_1-Field_B.

__________________________1 Table_2-Field_A Table_2-Field_B

__________________________2 Table_2-Field_A Table_2-Field_B

__________________________3 Table_2-Field_A Table_2-Field_B

__________________________4 Table_2-Field_A Table_2-Field_B

So,

if u give

AT NEW sbukrs

......

it'll check for all the fields till ' sbukrs ' ...i e -dmdate-dmtime- dmtyp-sbukrs- if any one field changes AT NEW will be true...

Mostly we use AT NEW on CHARACTER FIELD TYPES >..

C N D T

Inspire if needful..

Thank you,

Warm Regards,

Abhi...

Former Member
0 Kudos

Check sy-subrc eq 0. just after your each update statement.

if its not eq 0, update is getting failed.

this can happen it the record you want to update is locked by some other program.

you can use lock object to remove such things.

create lock objects ( SE11) for the table/s you are using then use it in the program and update / modify your table in side enqueue-Dequeue.

reward points it helpful.

Rgs,

Ashok

Edited by: Ashok Gupta on Mar 30, 2008 4:12 PM

Former Member
0 Kudos

Thankyou Naga Pavan and Abaper007. I have rewarded you good points.