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: 

deleting internal table records

Former Member
0 Kudos
3,499

Hi :

After the if condition, i want to delete records of an internal table which doesnt satisfy the condition.

i used " DELETE IT_TAB INDEX SY-TABIX.

its goin to runtime errors since index value should be greater than zero.

i declared

data : p_tabix like sy-tabix.

and inside the internal table loop,

p_tabix = sy-tabix.

after the if condition

delete it_tab index p_tabix..

its not goin to run time errors but never deletes the internal table records.

any input is rewarded.

Thnx.

Raghu

11 REPLIES 11

Former Member
0 Kudos
468

Hi Raghu,

The logic what ever you are using is correct, it should work. Otherwise you try to use other alternatives..as below...

- DELETE itab.

- DELETE itab INDEX idx.

- DELETE itab FROM idx1 TO idx2.

- DELETE itab WHERE cond.

Regards,

*DJ

reward, if its useful

former_member191977
Contributor
0 Kudos
468

Can you post your code fully.

If the condition you are checking is satisfied then it deletes the record.

Former Member
0 Kudos
468

Use like DELETE IT_TAB INDEX SY-index.

0 Kudos
468

Hi ,

this is the code.

here auart = PM01

Plant 3200.

abckz = C.

REPORT z_ka_scr NO STANDARD PAGE HEADING " LINE-SIZE 132.

MESSAGE-ID z_ka.

TYPE-POOLS : slis.

*-Tables----


TABLES: aufk, iloa,hivg,afru,plko, afvv,mpos,sscrfields,jest,tj02t.

*-Selection screen----


SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

SELECT-OPTIONS:

s_auart FOR aufk-auart, "Work Order type

s_tplnr FOR iloa-tplnr, "Functional Location

s_abckz FOR iloa-abckz, "ABC Indicator

s_date FOR aufk-idat2. "Date Range

SELECTION-SCREEN END OF BLOCK blk1.

SELECTION-SCREEN:BEGIN OF LINE,

PUSHBUTTON 2(25) text-020 USER-COMMAND cli1,

END OF LINE.

*-Global Types----


TYPES:BEGIN OF t_tab,

aufnr LIKE aufk-aufnr,

auart LIKE aufk-auart,

arbei LIKE plpo-arbei,

ismnw LIKE afru-ismnw,

v_status1(20) TYPE c,

ehrs(4) TYPE c,

stat(5) TYPE c,

aueru LIKE afru-aueru,

tplnr LIKE iloa-tplnr,

abckz LIKE iloa-abckz,

idat2 LIKE aufk-idat2,

END OF t_tab.

**-Global Structures / Internal Tables----


DATA : it_tab TYPE t_tab OCCURS 0 WITH HEADER LINE.

DATA: v_status LIKE bsvx-sttxt.

DATA: v_objnr LIKE aufk-objnr.

DATA : aufnr LIKE it_tab-aufnr.

DATA :auart LIKE it_tab-auart.

DATA : p_tabix LIKE sy-tabix.

DATA : v_status1 LIKE v_status.

DATA : v_cnt1(6) TYPE c,

v_cnt2(6) TYPE c,

v_cnt3(6) TYPE c.

v_cnt1 = 0.

v_cnt2 = 0.

v_cnt3 = 0.

DATA: l_repid TYPE sy-repid,

ls_fieldcat TYPE slis_fieldcat_alv, "Field Catalog list

aufk_fc TYPE slis_t_fieldcat_alv. "Field Catalog

LOAD-OF-PROGRAM.

l_repid = sy-repid.

*

*-Global Variables----


*data:

*

*-Constants----


*constants:

*

*-At Selection Screen_----


*at selection-screen.

AT SELECTION-SCREEN.

IF sscrfields-ucomm = 'CLI1'.

SUBMIT z_ka_scr VIA SELECTION-SCREEN

AND RETURN.

ENDIF.

*-Initialization----


*INITIALIZATION.

*

*

*-Main Program logic----


START-OF-SELECTION.

WRITE : /10 'WNO',25 'WTYPE', 35 'PLANHRS',45 'COMPLET',55 'EARNHRS',65 'ACTUAL',75 'SYS-STAT'.

SELECT aaufnr aauart barbei cismnw INTO CORRESPONDING FIELDS OF TABLE it_tab

FROM aufk AS a

INNER JOIN plpo AS b ON awerks = bwerks

INNER JOIN afru AS c ON awerks = cwerks

INNER JOIN iloa AS d ON abukrs = dbukrs

  • INNER JOIN HIKO AS E ON AWERKS = EWERKS

WHERE a~auart IN s_auart

AND a~auart NE 'PM04'

AND a~auart NE 'PM09'

AND a~werks = 'M600'

AND d~tplnr = 'PB-PRO-S01-BY02-MIL'

AND d~abckz IN s_abckz

  • AND A~BUKRS = '1546'

  • AND B~ARBEI = '2.0'

  • AND C~ISMNW = '5.0'

  • AND C~AUERU = 'X'

AND a~idat2 IN s_date.

IF sy-subrc <> 0.

MESSAGE i001.

RETURN.

ENDIF.

SORT it_tab BY aufnr.

DELETE ADJACENT DUPLICATES FROM it_tab COMPARING aufnr.

LOOP AT it_tab.

p_tabix = sy-tabix.

  • IF it_tab-ismnw >= it_tab-arbei.

  • it_tab-ehrs = it_tab-arbei.

  • it_tab-stat = 'YES'.

  • ELSE.

  • it_tab-ehrs = ''.

  • it_tab-stat = 'NO'.

  • ENDIF.

SELECT SINGLE objnr FROM aufk

INTO v_objnr

WHERE aufnr = it_tab-aufnr.

CALL FUNCTION 'STATUS_TEXT_EDIT'

EXPORTING

flg_user_stat = 'X'

objnr = v_objnr

only_active = 'X'

spras = sy-langu

IMPORTING

line = v_status.

  • exceptions

  • object_not_found = 01.

*

IF ( v_status CS 'CNF' OR v_status CS 'TECO' OR v_status CS 'CLSD' )

AND ( v_status NS 'PCNF' OR v_status NS 'CRTD' OR v_status NS 'DLFL' OR v_status NS 'REL' )

AND ( v_status NS 'REL' OR v_status NS 'PCNF' ) AND ( v_status CS 'TECO' OR v_status CS 'PCNF' ).

*

v_status1 = v_status.

DELETE it_tab WHERE v_status1 = 'v_status'.

WRITE : 75 v_status1.

ENDIF.

IF v_status CS 'CNF'.

IF it_tab-ismnw >= it_tab-arbei.

it_tab-ehrs = it_tab-arbei.

it_tab-stat = 'YES'.

ELSE.

it_tab-ehrs = ''.

it_tab-stat = 'NO'.

ENDIF.

ENDIF.

MODIFY it_tab.

  • write : /10 I_TAB1-AUFNR,20 I_TAB1-auart,30 I_TAB1-arbei,45 I_TAB1-STAT,55 I_TAB1-EHRS,65 I_TAB1-ismnw,75 V_STATUS.

WRITE : /10 it_tab-aufnr,20 it_tab-auart,30 it_tab-arbei,45 it_tab-stat,55 it_tab-ehrs,60 it_tab-ismnw.

v_cnt1 = v_cnt1 + it_tab-arbei.

v_cnt2 = v_cnt2 + it_tab-ehrs.

v_cnt3 = v_cnt3 + it_tab-ismnw.

ENDLOOP.

WRITE : /30 v_cnt1, 55 v_cnt2, 65 v_cnt3.

0 Kudos
468

DELETE it_tab WHERE v_status1 = 'v_status'.

It should be like

DELETE it_tab WHERE v_status1 = <b>v_status</b>. You should not mention a variable in single quotes. I think thats why delete is not successful.

Reward if its useful.

Regards,

Sail

0 Kudos
468

I don't see any delete statement with index. may be you chagned the code.

if that's the case then v_status, i guess its a variable is put in quotes is wrong as murugesan said in the previous answer.

correct that and check it out.

Former Member
0 Kudos
468

It's not really a good idea to add or delete records to an internal table during a loop. It's better to set a flag in the table and after the loop is finished delete the table where the flag is set.

Rob

0 Kudos
468

Hi Rob,

Can you provide me sample code to set a flag in the internal table.

Appreciate it.

Thanks.

Raghu

0 Kudos
468

add an extra field in your internal table(say itab) like status.

loop at itab.

if condition that you are checking for.

status = 'X'.

endif.

endloop.

delete itab where status = 'X'.

this will delete all the records in itab with status X.

reward if it's useful.

0 Kudos
468

Include one field flag in internal table.

Set the flag = 'X' per condition .

Outside the loop. DELETE itab where flag = 'X'.

0 Kudos
468

Maybe I'm missing something, but I don't see why you want to:


      v_status1 = v_status.
      DELETE it_tab WHERE v_status1 = v_status.
      WRITE : 75 v_status1.

Since you're in a loop on that table, can't you just replace that with


      DELETE it_tab.
      WRITE : 75 v_status.

This will only delete the current line and shouldn't interfere with the looping.

Rob