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

Delete record in database table does not work...

aris_hidalgo
Contributor
0 Likes
5,760

Hello Experts,

I am wondering why my delete statement does not work. I want to delete records in my database

table which is not found in my internal table. Below is my code:


SORT ts_selcriteria BY zattrrulid zattrseq zattrssq.

  LOOP AT is_ztxx_pt9126 ASSIGNING <fs_ztxx_pt9126>.

*"  Check if records was deleted in selection
*"  table. If yes, then delete this in table ZTXX_PT9126
    READ TABLE ts_selcriteria
      ASSIGNING <fs_selcriteria>
      WITH KEY zattrrulid = <fs_ztxx_pt9126>-zattrrulid
               zattrseq = <fs_ztxx_pt9126>-zattrseq
               zattrssq = <fs_ztxx_pt9126>-zattrssq
      BINARY SEARCH.

    IF sy-subrc ne 0.

      DELETE ztxx_pt9126 FROM <fs_ztxx_pt9126>.

    ENDIF.

  ENDLOOP.

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
3,152

Have you tried not using field symbol, but normal work area instead?

13 REPLIES 13
Read only

MarcinPciak
Active Contributor
0 Likes
3,153

Have you tried not using field symbol, but normal work area instead?

Read only

0 Likes
3,152

Yes, I also tried using work area but to no avail. Below is my updated code:


e_ztxx_pt9126 = <fs_ztxx_pt9126>.

      DELETE ztxx_pt9126 FROM e_ztxx_pt9126.

Read only

0 Likes
3,152

Is your table and work area type as table/line of Z DB table? If the strcutre is diffent then this might be the issue. Try giving key of entry you want to delete instead of giving work area.


DELETE ztxx_pt9126 FROM where field = e_ztxx_pt9126-field
                                                    and field2 = e_ztxx_pt9126-field2 
                                                     ...

Regards

Marcin

Read only

0 Likes
3,152

e_ztxx_pt9126 = <fs_ztxx_pt9126>.

DELETE ztxx_pt9126 FROM e_ztxx_pt9126.

in this your work area should be of same type as ztxx_pt9126.

and also use COMMIT WORK after delete stmt.

Read only

Former Member
0 Likes
3,152

Make sure the key fields match the DB-table exactly (cannot be derived from your post)

Make sure the entry is lockable.

Do a COMMIT WORK if the delete statement was successful and a ROLLBACK WORK if it was not. Check SY-SUBRC after the delete statement.

With kind regards,

Roel van den Berge

Read only

Former Member
0 Likes
3,152
LOOP AT is_ztxx_pt9126 ASSIGNING <fs_ztxx_pt9126>.

This is a database table? If it is, this statement didn't work. You loop at internal tables. So, you'd need to import the database into an internal table first....or

loop at the internal table.

select the corresponding, matching data from your db table.

if found, then do the delete on the db table.

end the loop.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,152

>

> This is a database table? If it is, this statement didn't work. You loop at internal tables. So, you'd need to import the database into an internal table first....or

Wow !!! And why do we need to select before deleting ?

Read only

0 Likes
3,152

Hi,

First of all, please check check first the code I posted before you comment. Anyway, I tried everything and still it doesnt work. I tried deleting from worka area, field symbol, etc and putting commit work but still the record is still not deleted.

Read only

0 Likes
3,152


types : begin of ty_jtab,
       f1(10) type c,
       end of ty_jtab.
data : jtab type standard table of ty_jtab with header line,
       itab type standard table of ty_jtab with header line.

data : wa_jtab type  ty_jtab,
       wa_itab type  ty_jtab.


*populate itab for say db
itab-f1 = '1000'. append itab.
itab-f1 = '2000'. append itab.
itab-f1 = '3000'. append itab.
*populate jtab
jtab-f1 = '1000'. append jtab.
jtab-f1 = '4000'. append jtab.
jtab-f1 = '8000'. append jtab.
jtab-f1 = '9000'. append jtab.


sort itab by f1.
sort jtab by f1.

clear: itab, wa_itab.

loop at itab into wa_itab.
  read table jtab into wa_jtab with key f1 = wa_itab-f1
                                         binary search.
  if sy-subrc ne 0.
    delete itab from wa_itab.
    if sy-subrc ne 0.
      write :/ 'fail', wa_itab-f1.
    endif.
  endif.
endloop.

*o/p is two times fail which shows the assumption.

br, 
vijay
Read only

Former Member
0 Likes
3,152

Why do you say it doesn't work? How do you know the record is not deleted? I don't see any checking of sy-subrc after the delete.

Rob

Read only

Former Member
0 Likes
3,152

field-symbols : <fsi> type ty_jtab,
                <fsj> type ty_jtab.
loop at itab assigning <fsi>.
  read table jtab assigning <fsj> with key f1 = <fsi>-f1
                                         binary search.
  if sy-subrc ne 0.
    delete itab from <fsi>.
    if sy-subrc ne 0.
      write :/ 'fail', <fsi>-f1.
    endif.
  endif.
endloop.

replaced with the fiedsymbols and its showing the same o/p as expected .debugging for sy-subrc after delete may lead to some help. The only thing that can go wrong is if there is any mismatch with the work area or any more key field is not included in wa can lead fail in the delete statement.

br,

Vijay

Read only

rainer_hbenthal
Active Contributor
0 Likes
3,152

>

> DELETE ztxx_pt9126 FROM <fs_ztxx_pt9126>.

>

This is not the correct syntax for deleting rows from a database table. Or do you really want to delete a complete cluster?

Read only

Former Member
0 Likes
3,152

Hi Aris (VirayLab),

I thought your senior in ABAP. Have you forgotten that pressing F1 to check for correct syntax to delete in a database table would have solved your issue instantly. Or best would have been asking your teammates (BJ or Analyn) or colleagues (Ice, Pao or Rodel).

You see, your problem was so much BASIC ABAP and you took some time posting message/question in SDN. Is this the reason why you refrain from providing timely email updates (because you are waiting for somebody from SDN to solve your issue)?

Also next time, please never ever share company specific/protected programs/coding/objects. Im sure you know that this is illegal.

Cheers,

Bully